From f0e137337e63f86060db3ff46a43d2b14dac11ed Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Thu, 26 Aug 2021 13:54:52 -0400 Subject: [PATCH 1/2] [Fix #7849] Add new `Lint/AmbiguousOperatorPrecedence` cop. --- ...add_new_lintambiguousoperatorprecedence.md | 1 + config/default.yml | 7 + lib/rubocop.rb | 1 + .../cop/lint/ambiguous_operator_precedence.rb | 107 +++++++++++++++ .../ambiguous_operator_precedence_spec.rb | 123 ++++++++++++++++++ 5 files changed, 239 insertions(+) create mode 100644 changelog/new_add_new_lintambiguousoperatorprecedence.md create mode 100644 lib/rubocop/cop/lint/ambiguous_operator_precedence.rb create mode 100644 spec/rubocop/cop/lint/ambiguous_operator_precedence_spec.rb diff --git a/changelog/new_add_new_lintambiguousoperatorprecedence.md b/changelog/new_add_new_lintambiguousoperatorprecedence.md new file mode 100644 index 00000000000..fe332d13a22 --- /dev/null +++ b/changelog/new_add_new_lintambiguousoperatorprecedence.md @@ -0,0 +1 @@ +* [#7849](https://github.com/rubocop/rubocop/issues/7849): Add new `Lint/AmbiguousOperatorPrecedence` cop. ([@dvandersluis][]) diff --git a/config/default.yml b/config/default.yml index 84e4078fa15..748a377a673 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1448,6 +1448,13 @@ Lint/AmbiguousOperator: VersionAdded: '0.17' VersionChanged: '0.83' +Lint/AmbiguousOperatorPrecedence: + Description: >- + Checks for expressions containing multiple binary operations with + ambiguous precedence. + Enabled: pending + VersionAdded: '<>' + Lint/AmbiguousRange: Description: Checks for ranges with ambiguous boundaries. Enabled: pending diff --git a/lib/rubocop.rb b/lib/rubocop.rb index ab6171ba700..1a19102b70b 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -263,6 +263,7 @@ require_relative 'rubocop/cop/lint/ambiguous_assignment' require_relative 'rubocop/cop/lint/ambiguous_block_association' require_relative 'rubocop/cop/lint/ambiguous_operator' +require_relative 'rubocop/cop/lint/ambiguous_operator_precedence' require_relative 'rubocop/cop/lint/ambiguous_range' require_relative 'rubocop/cop/lint/ambiguous_regexp_literal' require_relative 'rubocop/cop/lint/assignment_in_condition' diff --git a/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb b/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb new file mode 100644 index 00000000000..10969a5a346 --- /dev/null +++ b/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Lint + # This cop looks for expressions containing multiple binary operators + # where precedence is ambiguous due to lack of parentheses. For example, + # in `1 + 2 * 3`, the multiplication will happen before the addition, but + # lexically it appears that the addition will happen first. + # + # The cop does not consider unary operators (ie. `!a` or `-b`) or comparison + # operators (ie. `a =~ b`) because those are not ambiguous. + # + # NOTE: Ranges are handled by `Lint/AmbiguousRange`. + # + # @example + # # bad + # a + b * c + # a || b && c + # a ** b + c + # + # # good (different precedence) + # a + (b * c) + # a || (b && c) + # (a ** b) + c + # + # # good (same precedence) + # a + b + c + # a * b / c % d + class AmbiguousOperatorPrecedence < Base + extend AutoCorrector + + # See https://ruby-doc.org/core-3.0.2/doc/syntax/precedence_rdoc.html + PRECEDENCE = [ + %i[**], + %i[* / %], + %i[+ -], + %i[<< >>], + %i[&], + %i[| ^], + %i[&&], + %i[||] + ].freeze + RESTRICT_ON_SEND = PRECEDENCE.flatten.freeze + MSG = 'Wrap expressions with varying precedence with parentheses to avoid ambiguity.' + + def on_new_investigation + # Cache the precedence of each node being investigated + # so that we only need to calculate it once + @node_precedences = {} + super + end + + def on_and(node) + return unless (parent = node.parent) + + return if parent.begin_type? # if the `and` is in a `begin`, it's parenthesized already + return unless parent.or_type? + + add_offense(node) do |corrector| + autocorrect(corrector, node) + end + end + + def on_send(node) + return if node.parenthesized? + + return unless (parent = node.parent) + return unless operator?(parent) + return unless greater_precedence?(node, parent) + + add_offense(node) do |corrector| + autocorrect(corrector, node) + end + end + + private + + def precedence(node) + @node_precedences.fetch(node) do + PRECEDENCE.index { |operators| operators.include?(operator_name(node)) } + end + end + + def operator?(node) + (node.send_type? && RESTRICT_ON_SEND.include?(node.method_name)) || node.operator_keyword? + end + + def greater_precedence?(node1, node2) + precedence(node2) > precedence(node1) + end + + def operator_name(node) + if node.send_type? + node.method_name + else + node.operator.to_sym + end + end + + def autocorrect(corrector, node) + corrector.wrap(node, '(', ')') + end + end + end + end +end diff --git a/spec/rubocop/cop/lint/ambiguous_operator_precedence_spec.rb b/spec/rubocop/cop/lint/ambiguous_operator_precedence_spec.rb new file mode 100644 index 00000000000..1d653212e7f --- /dev/null +++ b/spec/rubocop/cop/lint/ambiguous_operator_precedence_spec.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Lint::AmbiguousOperatorPrecedence, :config do + it 'does not register an offense when there is only one operator in the expression' do + expect_no_offenses(<<~RUBY) + a + b + RUBY + end + + it 'does not register an offense when all operators in the expression have the same precedence' do + expect_no_offenses(<<~RUBY) + a + b + c + a * b / c % d + a && b && c + RUBY + end + + it 'does not register an offense when expressions are wrapped in parentheses by precedence' do + expect_no_offenses(<<~RUBY) + a + (b * c) + (a ** b) + c + RUBY + end + + it 'does not register an offense when expressions are wrapped in parentheses by reverse precedence' do + expect_no_offenses(<<~RUBY) + (a + b) * c + a ** (b + c) + RUBY + end + + it 'does not register an offense when boolean expressions are wrapped in parens' do + expect_no_offenses(<<~RUBY) + (a && b) || c + a || (b & c) + RUBY + end + + it 'registers an offense when an expression with mixed precedence has no parens' do + expect_offense(<<~RUBY) + a + b * c + ^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + RUBY + + expect_correction(<<~RUBY) + a + (b * c) + RUBY + end + + it 'registers an offense when an expression with mixed boolean operators has no parens' do + expect_offense(<<~RUBY) + a && b || c + ^^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + a || b && c + ^^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + RUBY + + expect_correction(<<~RUBY) + (a && b) || c + a || (b && c) + RUBY + end + + it 'registers an offense for expressions containing booleans and operators' do + expect_offense(<<~RUBY) + a && b * c + ^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + a * b && c + ^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + RUBY + + expect_correction(<<~RUBY) + a && (b * c) + (a * b) && c + RUBY + end + + it 'registers an offense when the entire expression is wrapped in parentheses' do + expect_offense(<<~RUBY) + (a + b * c ** d) + ^^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + ^^^^^^^^^^ Wrap expressions with varying precedence with parentheses to avoid ambiguity. + RUBY + + expect_correction(<<~RUBY) + (a + (b * (c ** d))) + RUBY + end + + it 'corrects a super long expression in precedence order' do + expect_offense(<<~RUBY) + a ** b * c / d % e + f - g << h >> i & j | k ^ l && m || n + ^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + RUBY + + expect_correction(<<~RUBY) + (((((((a ** b) * c / d % e) + f - g) << h >> i) & j) | k ^ l) && m) || n + RUBY + end + + it 'corrects a super long expression in reverse precedence order' do + expect_offense(<<~RUBY) + a || b && c | d ^ e & f << g >> h + i - j * k / l % n ** m + ^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Wrap expressions [...] + RUBY + + expect_correction(<<~RUBY) + a || (b && (c | d ^ (e & (f << g >> (h + i - (j * k / l % (n ** m))))))) + RUBY + end +end From b39ba73d0f0ef30c0aa6df35c119fbd31f992caf Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Thu, 26 Aug 2021 15:03:59 -0400 Subject: [PATCH 2/2] Correct `Lint/AmbiguousOperatorPrecedence` offenses. --- lib/rubocop/cop/base.rb | 4 ++-- .../correctors/lambda_literal_to_method_corrector.rb | 4 ++-- lib/rubocop/cop/correctors/line_break_corrector.rb | 2 +- lib/rubocop/cop/layout/argument_alignment.rb | 2 +- lib/rubocop/cop/layout/class_structure.rb | 3 ++- lib/rubocop/cop/layout/end_alignment.rb | 2 +- lib/rubocop/cop/layout/leading_comment_space.rb | 2 +- lib/rubocop/cop/layout/line_length.rb | 2 +- lib/rubocop/cop/layout/multiline_block_layout.rb | 2 +- .../space_around_equals_in_parameter_default.rb | 3 ++- lib/rubocop/cop/layout/space_around_keyword.rb | 4 ++-- .../cop/layout/space_inside_reference_brackets.rb | 2 +- lib/rubocop/cop/lint/ambiguous_range.rb | 2 +- lib/rubocop/cop/lint/empty_in_pattern.rb | 2 +- lib/rubocop/cop/lint/erb_new_arguments.rb | 2 +- lib/rubocop/cop/lint/float_out_of_range.rb | 2 +- lib/rubocop/cop/lint/unused_method_argument.rb | 5 ++--- lib/rubocop/cop/lint/useless_times.rb | 2 +- lib/rubocop/cop/metrics/perceived_complexity.rb | 2 +- lib/rubocop/cop/metrics/utils/abc_size_calculator.rb | 2 +- .../cop/metrics/utils/code_length_calculator.rb | 2 +- lib/rubocop/cop/mixin/code_length.rb | 2 +- .../cop/mixin/multiline_expression_indentation.rb | 4 ++-- lib/rubocop/cop/naming/constant_name.rb | 2 +- lib/rubocop/cop/style/accessor_grouping.rb | 4 ++-- lib/rubocop/cop/style/collection_methods.rb | 3 ++- .../cop/style/document_dynamic_eval_definition.rb | 2 +- lib/rubocop/cop/style/documentation.rb | 6 +++--- lib/rubocop/cop/style/empty_method.rb | 2 +- .../cop/style/if_with_boolean_literal_branches.rb | 5 +++-- lib/rubocop/cop/style/lambda_call.rb | 2 +- .../omit_parentheses.rb | 12 ++++++------ lib/rubocop/cop/style/mutable_constant.rb | 2 +- lib/rubocop/cop/style/negated_if.rb | 2 +- lib/rubocop/cop/style/negated_unless.rb | 2 +- lib/rubocop/cop/style/non_nil_check.rb | 4 ++-- lib/rubocop/cop/style/not.rb | 4 ++-- lib/rubocop/cop/style/parallel_assignment.rb | 2 +- lib/rubocop/cop/style/percent_q_literals.rb | 4 ++-- lib/rubocop/cop/style/raise_args.rb | 2 +- lib/rubocop/cop/style/redundant_condition.rb | 5 ++--- lib/rubocop/cop/style/redundant_interpolation.rb | 2 +- lib/rubocop/cop/style/redundant_percent_q.rb | 5 ++--- .../cop/style/redundant_self_assignment_branch.rb | 2 +- lib/rubocop/cop/style/regexp_literal.rb | 6 +++--- lib/rubocop/cop/style/return_nil.rb | 3 ++- lib/rubocop/cop/style/static_class.rb | 3 +-- lib/rubocop/cop/style/string_concatenation.rb | 2 +- lib/rubocop/cop/style/trivial_accessors.rb | 2 +- lib/rubocop/cop/style/yoda_condition.rb | 6 +++--- lib/rubocop/result_cache.rb | 2 +- lib/rubocop/runner.rb | 3 +-- spec/rubocop/formatter/progress_formatter_spec.rb | 6 +++--- spec/rubocop/formatter/tap_formatter_spec.rb | 6 +++--- 54 files changed, 86 insertions(+), 86 deletions(-) diff --git a/lib/rubocop/cop/base.rb b/lib/rubocop/cop/base.rb index da74f7d55b7..27a49f749b4 100644 --- a/lib/rubocop/cop/base.rb +++ b/lib/rubocop/cop/base.rb @@ -222,8 +222,8 @@ def target_rails_version def relevant_file?(file) file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME || - file_name_matches_any?(file, 'Include', true) && - !file_name_matches_any?(file, 'Exclude', false) + (file_name_matches_any?(file, 'Include', true) && + !file_name_matches_any?(file, 'Exclude', false)) end def excluded_file?(file) diff --git a/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb b/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb index 70c8432376b..d60797ead93 100644 --- a/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +++ b/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb @@ -87,8 +87,8 @@ def lambda_arg_string end def needs_separating_space? - block_begin.begin_pos == arguments_end_pos && - selector_end.end_pos == arguments_begin_pos || + (block_begin.begin_pos == arguments_end_pos && + selector_end.end_pos == arguments_begin_pos) || block_begin.begin_pos == selector_end.end_pos end diff --git a/lib/rubocop/cop/correctors/line_break_corrector.rb b/lib/rubocop/cop/correctors/line_break_corrector.rb index 7b5b14568cd..55c4b2df9e9 100644 --- a/lib/rubocop/cop/correctors/line_break_corrector.rb +++ b/lib/rubocop/cop/correctors/line_break_corrector.rb @@ -28,7 +28,7 @@ def break_line_before(range:, node:, corrector:, configured_width:, indent_steps: 1) corrector.insert_before( range, - "\n#{' ' * (node.loc.keyword.column + indent_steps * configured_width)}" + "\n#{' ' * (node.loc.keyword.column + (indent_steps * configured_width))}" ) end diff --git a/lib/rubocop/cop/layout/argument_alignment.rb b/lib/rubocop/cop/layout/argument_alignment.rb index 2feebfc63ea..123bfd4589a 100644 --- a/lib/rubocop/cop/layout/argument_alignment.rb +++ b/lib/rubocop/cop/layout/argument_alignment.rb @@ -54,7 +54,7 @@ class ArgumentAlignment < Base def on_send(node) first_arg = node.first_argument - return if !multiple_arguments?(node, first_arg) || node.send_type? && node.method?(:[]=) + return if !multiple_arguments?(node, first_arg) || (node.send_type? && node.method?(:[]=)) if first_arg.hash_type? && !first_arg.braces? pairs = first_arg.pairs diff --git a/lib/rubocop/cop/layout/class_structure.rb b/lib/rubocop/cop/layout/class_structure.rb index 56680d62ed7..f880b31b2a9 100644 --- a/lib/rubocop/cop/layout/class_structure.rb +++ b/lib/rubocop/cop/layout/class_structure.rb @@ -264,7 +264,8 @@ def humanize_node(node) def source_range_with_comment(node) begin_pos, end_pos = - if node.def_type? && !node.method?(:initialize) || node.send_type? && node.def_modifier? + if (node.def_type? && !node.method?(:initialize)) || + (node.send_type? && node.def_modifier?) start_node = find_visibility_start(node) || node end_node = find_visibility_end(node) || node [begin_pos_with_comment(start_node), diff --git a/lib/rubocop/cop/layout/end_alignment.rb b/lib/rubocop/cop/layout/end_alignment.rb index 598563a15d1..c5f36ce59c8 100644 --- a/lib/rubocop/cop/layout/end_alignment.rb +++ b/lib/rubocop/cop/layout/end_alignment.rb @@ -182,7 +182,7 @@ def alignment_node_for_variable_style(node) def assignment_or_operator_method(node) node.ancestors.find do |ancestor| - ancestor.assignment_or_similar? || ancestor.send_type? && ancestor.operator_method? + ancestor.assignment_or_similar? || (ancestor.send_type? && ancestor.operator_method?) end end end diff --git a/lib/rubocop/cop/layout/leading_comment_space.rb b/lib/rubocop/cop/layout/leading_comment_space.rb index 7cca1b14291..726e434fbc8 100644 --- a/lib/rubocop/cop/layout/leading_comment_space.rb +++ b/lib/rubocop/cop/layout/leading_comment_space.rb @@ -77,7 +77,7 @@ def hash_mark(expr) end def allowed_on_first_line?(comment) - shebang?(comment) || rackup_config_file? && rackup_options?(comment) + shebang?(comment) || (rackup_config_file? && rackup_options?(comment)) end def shebang?(comment) diff --git a/lib/rubocop/cop/layout/line_length.rb b/lib/rubocop/cop/layout/line_length.rb index 8a166d06a97..cb50a69639e 100644 --- a/lib/rubocop/cop/layout/line_length.rb +++ b/lib/rubocop/cop/layout/line_length.rb @@ -176,7 +176,7 @@ def check_line(line, line_index) def ignored_line?(line, line_index) matches_ignored_pattern?(line) || shebang?(line, line_index) || - heredocs && line_in_permitted_heredoc?(line_index.succ) + (heredocs && line_in_permitted_heredoc?(line_index.succ)) end def shebang?(line, line_index) diff --git a/lib/rubocop/cop/layout/multiline_block_layout.rb b/lib/rubocop/cop/layout/multiline_block_layout.rb index 79339ec4e1c..62064caf5a2 100644 --- a/lib/rubocop/cop/layout/multiline_block_layout.rb +++ b/lib/rubocop/cop/layout/multiline_block_layout.rb @@ -89,7 +89,7 @@ def characters_needed_for_space_and_pipes(node) if node.source.lines.first.end_with?("|\n") PIPE_SIZE else - 1 + PIPE_SIZE * 2 + 1 + (PIPE_SIZE * 2) end end diff --git a/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb b/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb index c7853c2c394..b88e9bd7442 100644 --- a/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +++ b/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb @@ -47,7 +47,8 @@ def check_optarg(arg, equals, value) space_on_both_sides = space_on_both_sides?(arg, equals) no_surrounding_space = no_surrounding_space?(arg, equals) - if style == :space && space_on_both_sides || style == :no_space && no_surrounding_space + if (style == :space && space_on_both_sides) || + (style == :no_space && no_surrounding_space) correct_style_detected else incorrect_style_detected(arg, value) diff --git a/lib/rubocop/cop/layout/space_around_keyword.rb b/lib/rubocop/cop/layout/space_around_keyword.rb index 60749bdb8f8..0a7fe3bdfc1 100644 --- a/lib/rubocop/cop/layout/space_around_keyword.rb +++ b/lib/rubocop/cop/layout/space_around_keyword.rb @@ -228,8 +228,8 @@ def space_after_missing?(range) def accepted_opening_delimiter?(range, char) return true unless char - accept_left_square_bracket?(range) && char == '[' || - accept_left_parenthesis?(range) && char == '(' + (accept_left_square_bracket?(range) && char == '[') || + (accept_left_parenthesis?(range) && char == '(') end def accept_left_parenthesis?(range) diff --git a/lib/rubocop/cop/layout/space_inside_reference_brackets.rb b/lib/rubocop/cop/layout/space_inside_reference_brackets.rb index f75ea152992..966fe46e2db 100644 --- a/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +++ b/lib/rubocop/cop/layout/space_inside_reference_brackets.rb @@ -107,7 +107,7 @@ def left_ref_bracket(node, tokens) current_token = tokens.reverse.find(&:left_ref_bracket?) previous_token = previous_token(current_token) - if node.method?(:[]=) || previous_token && !previous_token.right_bracket? + if node.method?(:[]=) || (previous_token && !previous_token.right_bracket?) tokens.find(&:left_ref_bracket?) else current_token diff --git a/lib/rubocop/cop/lint/ambiguous_range.rb b/lib/rubocop/cop/lint/ambiguous_range.rb index f3ace9674bd..eb4b106544c 100644 --- a/lib/rubocop/cop/lint/ambiguous_range.rb +++ b/lib/rubocop/cop/lint/ambiguous_range.rb @@ -83,7 +83,7 @@ def acceptable?(node) node.begin_type? || node.basic_literal? || node.variable? || node.const_type? || - node.call_type? && acceptable_call?(node) + (node.call_type? && acceptable_call?(node)) end def acceptable_call?(node) diff --git a/lib/rubocop/cop/lint/empty_in_pattern.rb b/lib/rubocop/cop/lint/empty_in_pattern.rb index 08ba49f8b38..0b51c36fce9 100644 --- a/lib/rubocop/cop/lint/empty_in_pattern.rb +++ b/lib/rubocop/cop/lint/empty_in_pattern.rb @@ -51,7 +51,7 @@ class EmptyInPattern < Base def on_case_match(node) node.in_pattern_branches.each do |branch| - next if branch.body || cop_config['AllowComments'] && comment_lines?(node) + next if branch.body || (cop_config['AllowComments'] && comment_lines?(node)) add_offense(branch) end diff --git a/lib/rubocop/cop/lint/erb_new_arguments.rb b/lib/rubocop/cop/lint/erb_new_arguments.rb index d509283273a..1e3816fa6ae 100644 --- a/lib/rubocop/cop/lint/erb_new_arguments.rb +++ b/lib/rubocop/cop/lint/erb_new_arguments.rb @@ -118,7 +118,7 @@ def autocorrect(corrector, node) end def correct_arguments?(arguments) - arguments.size == 1 || arguments.size == 2 && arguments[1].hash_type? + arguments.size == 1 || (arguments.size == 2 && arguments[1].hash_type?) end def build_kwargs(node) diff --git a/lib/rubocop/cop/lint/float_out_of_range.rb b/lib/rubocop/cop/lint/float_out_of_range.rb index fb46e938ede..3f930efe40d 100644 --- a/lib/rubocop/cop/lint/float_out_of_range.rb +++ b/lib/rubocop/cop/lint/float_out_of_range.rb @@ -24,7 +24,7 @@ class FloatOutOfRange < Base def on_float(node) value, = *node - return unless value.infinite? || value.zero? && /[1-9]/.match?(node.source) + return unless value.infinite? || (value.zero? && /[1-9]/.match?(node.source)) add_offense(node) end diff --git a/lib/rubocop/cop/lint/unused_method_argument.rb b/lib/rubocop/cop/lint/unused_method_argument.rb index 55dfa188e05..175a0c4ace1 100644 --- a/lib/rubocop/cop/lint/unused_method_argument.rb +++ b/lib/rubocop/cop/lint/unused_method_argument.rb @@ -87,9 +87,8 @@ def check_argument(variable) end def ignored_method?(body) - cop_config['IgnoreEmptyMethods'] && body.nil? || - cop_config['IgnoreNotImplementedMethods'] && - not_implemented?(body) + (cop_config['IgnoreEmptyMethods'] && body.nil?) || + (cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body)) end def message(variable) diff --git a/lib/rubocop/cop/lint/useless_times.rb b/lib/rubocop/cop/lint/useless_times.rb index 8c7944c08e2..6aa5b3f35da 100644 --- a/lib/rubocop/cop/lint/useless_times.rb +++ b/lib/rubocop/cop/lint/useless_times.rb @@ -65,7 +65,7 @@ def on_send(node) private def never_process?(count, node) - count < 1 || node.block_type? && node.body.nil? + count < 1 || (node.block_type? && node.body.nil?) end def remove_node(corrector, node) diff --git a/lib/rubocop/cop/metrics/perceived_complexity.rb b/lib/rubocop/cop/metrics/perceived_complexity.rb index 770b7ed8ca2..80fc9476bef 100644 --- a/lib/rubocop/cop/metrics/perceived_complexity.rb +++ b/lib/rubocop/cop/metrics/perceived_complexity.rb @@ -45,7 +45,7 @@ def complexity_score_for(node) else # Otherwise, the case node gets 0.8 complexity points and each # when gets 0.2. - (0.8 + 0.2 * nb_branches).round + (0.8 + (0.2 * nb_branches)).round end when :if node.else? && !node.elsif? ? 2 : 1 diff --git a/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb b/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb index 869b586a1f5..f6a43b97ecb 100644 --- a/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +++ b/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb @@ -46,7 +46,7 @@ def calculate visit_depth_last(@node) { |child| calculate_node(child) } [ - Math.sqrt(@assignment**2 + @branch**2 + @condition**2).round(2), + Math.sqrt((@assignment**2) + (@branch**2) + (@condition**2)).round(2), "<#{@assignment}, #{@branch}, #{@condition}>" ] end diff --git a/lib/rubocop/cop/metrics/utils/code_length_calculator.rb b/lib/rubocop/cop/metrics/utils/code_length_calculator.rb index e00504daf71..54236a559b9 100644 --- a/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +++ b/lib/rubocop/cop/metrics/utils/code_length_calculator.rb @@ -147,7 +147,7 @@ def extract_body(node) # Returns true for lines that shall not be included in the count. def irrelevant_line?(source_line) - source_line.blank? || !count_comments? && comment_line?(source_line) + source_line.blank? || (!count_comments? && comment_line?(source_line)) end def count_comments? diff --git a/lib/rubocop/cop/mixin/code_length.rb b/lib/rubocop/cop/mixin/code_length.rb index 1bdbfb16b47..bfb87c07219 100644 --- a/lib/rubocop/cop/mixin/code_length.rb +++ b/lib/rubocop/cop/mixin/code_length.rb @@ -43,7 +43,7 @@ def check_code_length(node) # Returns true for lines that shall not be included in the count. def irrelevant_line(source_line) - source_line.blank? || !count_comments? && comment_line?(source_line) + source_line.blank? || (!count_comments? && comment_line?(source_line)) end def build_code_length_calculator(node) diff --git a/lib/rubocop/cop/mixin/multiline_expression_indentation.rb b/lib/rubocop/cop/mixin/multiline_expression_indentation.rb index 2e7635d1931..e44e0f324f5 100644 --- a/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +++ b/lib/rubocop/cop/mixin/multiline_expression_indentation.rb @@ -134,7 +134,7 @@ def argument_in_method_call(node, kind) # rubocop:todo Metrics/CyclomaticComplex next if a.setter_method? next unless kind == :with_or_without_parentheses || - kind == :with_parentheses && parentheses?(a) + (kind == :with_parentheses && parentheses?(a)) a.arguments.any? { |arg| within_node?(node, arg) } end @@ -156,7 +156,7 @@ def part_of_assignment_rhs(node, candidate) def disqualified_rhs?(candidate, ancestor) UNALIGNED_RHS_TYPES.include?(ancestor.type) || - ancestor.block_type? && part_of_block_body?(candidate, ancestor) + (ancestor.block_type? && part_of_block_body?(candidate, ancestor)) end def valid_rhs?(candidate, ancestor) diff --git a/lib/rubocop/cop/naming/constant_name.rb b/lib/rubocop/cop/naming/constant_name.rb index b43b6725df6..1226787a664 100644 --- a/lib/rubocop/cop/naming/constant_name.rb +++ b/lib/rubocop/cop/naming/constant_name.rb @@ -54,7 +54,7 @@ def on_casgn(node) private def allowed_assignment?(value) - value && %i[block const casgn].include?(value.type) || + (value && %i[block const casgn].include?(value.type)) || allowed_method_call_on_rhs?(value) || class_or_struct_return_method?(value) || allowed_conditional_expression_on_rhs?(value) diff --git a/lib/rubocop/cop/style/accessor_grouping.rb b/lib/rubocop/cop/style/accessor_grouping.rb index c2ace18c354..1769551d86c 100644 --- a/lib/rubocop/cop/style/accessor_grouping.rb +++ b/lib/rubocop/cop/style/accessor_grouping.rb @@ -59,8 +59,8 @@ def on_class(node) def check(send_node) return if previous_line_comment?(send_node) - return unless grouped_style? && sibling_accessors(send_node).size > 1 || - separated_style? && send_node.arguments.size > 1 + return unless (grouped_style? && sibling_accessors(send_node).size > 1) || + (separated_style? && send_node.arguments.size > 1) message = message(send_node) add_offense(send_node, message: message) do |corrector| diff --git a/lib/rubocop/cop/style/collection_methods.rb b/lib/rubocop/cop/style/collection_methods.rb index 616b65f9e50..e72dd5ca059 100644 --- a/lib/rubocop/cop/style/collection_methods.rb +++ b/lib/rubocop/cop/style/collection_methods.rb @@ -68,7 +68,8 @@ def implicit_block?(node) return false unless node.arguments.any? node.last_argument.block_pass_type? || - node.last_argument.sym_type? && methods_accepting_symbol.include?(node.method_name.to_s) + (node.last_argument.sym_type? && + methods_accepting_symbol.include?(node.method_name.to_s)) end def message(node) diff --git a/lib/rubocop/cop/style/document_dynamic_eval_definition.rb b/lib/rubocop/cop/style/document_dynamic_eval_definition.rb index 1504e40df12..cbb827482a4 100644 --- a/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +++ b/lib/rubocop/cop/style/document_dynamic_eval_definition.rb @@ -86,7 +86,7 @@ def on_send(node) return unless arg_node&.dstr_type? && interpolated?(arg_node) return if inline_comment_docs?(arg_node) || - arg_node.heredoc? && comment_block_docs?(arg_node) + (arg_node.heredoc? && comment_block_docs?(arg_node)) add_offense(node.loc.selector) end diff --git a/lib/rubocop/cop/style/documentation.rb b/lib/rubocop/cop/style/documentation.rb index 26335cded44..dde2f1ecb01 100644 --- a/lib/rubocop/cop/style/documentation.rb +++ b/lib/rubocop/cop/style/documentation.rb @@ -109,12 +109,12 @@ def check(node, body, type) def nodoc_self_or_outer_module?(node) nodoc_comment?(node) || - compact_namespace?(node) && nodoc_comment?(outer_module(node).first) + (compact_namespace?(node) && nodoc_comment?(outer_module(node).first)) end def macro_only?(body) - body.respond_to?(:macro?) && body.macro? || - body.respond_to?(:children) && body.children&.all? { |child| macro_only?(child) } + (body.respond_to?(:macro?) && body.macro?) || + (body.respond_to?(:children) && body.children&.all? { |child| macro_only?(child) }) end def namespace?(node) diff --git a/lib/rubocop/cop/style/empty_method.rb b/lib/rubocop/cop/style/empty_method.rb index 5385e74481b..79826becf59 100644 --- a/lib/rubocop/cop/style/empty_method.rb +++ b/lib/rubocop/cop/style/empty_method.rb @@ -62,7 +62,7 @@ def message(_range) end def correct_style?(node) - compact_style? && compact?(node) || expanded_style? && expanded?(node) + (compact_style? && compact?(node)) || (expanded_style? && expanded?(node)) end def corrected(node) diff --git a/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb b/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb index 3edf27b4e89..aac7d7ffc6c 100644 --- a/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +++ b/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb @@ -109,12 +109,13 @@ def replacement_condition(node, condition) end def opposite_condition?(node) - !node.unless? && node.if_branch.false_type? || node.unless? && node.if_branch.true_type? + (!node.unless? && node.if_branch.false_type?) || + (node.unless? && node.if_branch.true_type?) end def require_parentheses?(condition) condition.and_type? || condition.or_type? || - condition.send_type? && condition.comparison_method? + (condition.send_type? && condition.comparison_method?) end end end diff --git a/lib/rubocop/cop/style/lambda_call.rb b/lib/rubocop/cop/style/lambda_call.rb index f977a6b2290..6023272aeff 100644 --- a/lib/rubocop/cop/style/lambda_call.rb +++ b/lib/rubocop/cop/style/lambda_call.rb @@ -52,7 +52,7 @@ def autocorrect(corrector, node) private def offense?(node) - explicit_style? && node.implicit_call? || implicit_style? && !node.implicit_call? + (explicit_style? && node.implicit_call?) || (implicit_style? && !node.implicit_call?) end def message(_node) diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb index 83e064a3d92..ab478ef92eb 100644 --- a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb @@ -93,8 +93,8 @@ def call_in_logical_operators?(node) parent = node.parent&.block_type? ? node.parent.parent : node.parent parent && (logical_operator?(parent) || - parent.send_type? && - parent.arguments.any? { |argument| logical_operator?(argument) }) + (parent.send_type? && + parent.arguments.any? { |argument| logical_operator?(argument) })) end def call_in_optional_arguments?(node) @@ -122,14 +122,14 @@ def call_with_braced_block?(node) def call_as_argument_or_chain?(node) node.parent && - (node.parent.send_type? && !assigned_before?(node.parent, node) || + ((node.parent.send_type? && !assigned_before?(node.parent, node)) || node.parent.csend_type? || node.parent.super_type? || node.parent.yield_type?) end def hash_literal_in_arguments?(node) node.arguments.any? do |n| hash_literal?(n) || - n.send_type? && node.descendants.any? { |descendant| hash_literal?(descendant) } + (n.send_type? && node.descendants.any? { |descendant| hash_literal?(descendant) }) end end @@ -171,8 +171,8 @@ def regexp_slash_literal?(node) end def unary_literal?(node) - node.numeric_type? && node.sign? || - node.parent&.send_type? && node.parent&.unary_operation? + (node.numeric_type? && node.sign?) || + (node.parent&.send_type? && node.parent&.unary_operation?) end def assigned_before?(node, target) diff --git a/lib/rubocop/cop/style/mutable_constant.rb b/lib/rubocop/cop/style/mutable_constant.rb index 3801ce3cc30..a27f80ee3e6 100644 --- a/lib/rubocop/cop/style/mutable_constant.rb +++ b/lib/rubocop/cop/style/mutable_constant.rb @@ -153,7 +153,7 @@ def strict_check(value) def check(value) range_enclosed_in_parentheses = range_enclosed_in_parentheses?(value) return unless mutable_literal?(value) || - target_ruby_version <= 2.7 && range_enclosed_in_parentheses + (target_ruby_version <= 2.7 && range_enclosed_in_parentheses) return if frozen_string_literal?(value) return if shareable_constant_value?(value) diff --git a/lib/rubocop/cop/style/negated_if.rb b/lib/rubocop/cop/style/negated_if.rb index 47ab0bf0b89..1b93b0b1752 100644 --- a/lib/rubocop/cop/style/negated_if.rb +++ b/lib/rubocop/cop/style/negated_if.rb @@ -90,7 +90,7 @@ def message(node) end def correct_style?(node) - style == :prefix && node.modifier_form? || style == :postfix && !node.modifier_form? + (style == :prefix && node.modifier_form?) || (style == :postfix && !node.modifier_form?) end end end diff --git a/lib/rubocop/cop/style/negated_unless.rb b/lib/rubocop/cop/style/negated_unless.rb index 46814e3a546..d07d631476a 100644 --- a/lib/rubocop/cop/style/negated_unless.rb +++ b/lib/rubocop/cop/style/negated_unless.rb @@ -80,7 +80,7 @@ def message(node) end def correct_style?(node) - style == :prefix && node.modifier_form? || style == :postfix && !node.modifier_form? + (style == :prefix && node.modifier_form?) || (style == :postfix && !node.modifier_form?) end end end diff --git a/lib/rubocop/cop/style/non_nil_check.rb b/lib/rubocop/cop/style/non_nil_check.rb index 2108dae0ce2..6c3d68796f2 100644 --- a/lib/rubocop/cop/style/non_nil_check.rb +++ b/lib/rubocop/cop/style/non_nil_check.rb @@ -63,7 +63,7 @@ class NonNilCheck < Base def on_send(node) return if ignored_node?(node) || - !include_semantic_changes? && nil_comparison_style == 'comparison' + (!include_semantic_changes? && nil_comparison_style == 'comparison') return unless register_offense?(node) message = message(node) @@ -87,7 +87,7 @@ def on_def(node) def register_offense?(node) not_equal_to_nil?(node) || - include_semantic_changes? && (not_and_nil_check?(node) || unless_and_nil_check?(node)) + (include_semantic_changes? && (not_and_nil_check?(node) || unless_and_nil_check?(node))) end def autocorrect(corrector, node) diff --git a/lib/rubocop/cop/style/not.rb b/lib/rubocop/cop/style/not.rb index 330c94b8c29..278d5485b6a 100644 --- a/lib/rubocop/cop/style/not.rb +++ b/lib/rubocop/cop/style/not.rb @@ -53,8 +53,8 @@ def opposite_method?(child) def requires_parens?(child) child.and_type? || child.or_type? || - child.send_type? && child.binary_operation? || - child.if_type? && child.ternary? + (child.send_type? && child.binary_operation?) || + (child.if_type? && child.ternary?) end def correct_opposite_method(corrector, range, child) diff --git a/lib/rubocop/cop/style/parallel_assignment.rb b/lib/rubocop/cop/style/parallel_assignment.rb index 584797a3853..92da0cd6f95 100644 --- a/lib/rubocop/cop/style/parallel_assignment.rb +++ b/lib/rubocop/cop/style/parallel_assignment.rb @@ -153,7 +153,7 @@ def tsort_each_child(assignment) def dependency?(lhs, rhs) uses_var?(rhs, var_name(lhs)) || - lhs.send_type? && lhs.assignment_method? && accesses?(rhs, lhs) + (lhs.send_type? && lhs.assignment_method? && accesses?(rhs, lhs)) end # `lhs` is an assignment method call like `obj.attr=` or `ary[idx]=`. diff --git a/lib/rubocop/cop/style/percent_q_literals.rb b/lib/rubocop/cop/style/percent_q_literals.rb index 5d2ea224b9e..464bf1559d1 100644 --- a/lib/rubocop/cop/style/percent_q_literals.rb +++ b/lib/rubocop/cop/style/percent_q_literals.rb @@ -53,8 +53,8 @@ def on_percent_literal(node) end def correct_literal_style?(node) - style == :lower_case_q && type(node) == '%q' || - style == :upper_case_q && type(node) == '%Q' + (style == :lower_case_q && type(node) == '%q') || + (style == :upper_case_q && type(node) == '%Q') end def message(_range) diff --git a/lib/rubocop/cop/style/raise_args.rb b/lib/rubocop/cop/style/raise_args.rb index 4f5b35d80a7..be3373a4004 100644 --- a/lib/rubocop/cop/style/raise_args.rb +++ b/lib/rubocop/cop/style/raise_args.rb @@ -141,7 +141,7 @@ def allowed_non_exploded_type?(arg) end def requires_parens?(parent) - parent.and_type? || parent.or_type? || parent.if_type? && parent.ternary? + parent.and_type? || parent.or_type? || (parent.if_type? && parent.ternary?) end end end diff --git a/lib/rubocop/cop/style/redundant_condition.rb b/lib/rubocop/cop/style/redundant_condition.rb index 8ac7bee17e3..54df5fe5a04 100644 --- a/lib/rubocop/cop/style/redundant_condition.rb +++ b/lib/rubocop/cop/style/redundant_condition.rb @@ -120,11 +120,10 @@ def correct_ternary(corrector, node) end def require_parentheses?(node) - node.basic_conditional? && - node.modifier_form? || + (node.basic_conditional? && node.modifier_form?) || node.range_type? || node.rescue_type? || - node.respond_to?(:semantic_operator?) && node.semantic_operator? + (node.respond_to?(:semantic_operator?) && node.semantic_operator?) end def without_argument_parentheses_method?(node) diff --git a/lib/rubocop/cop/style/redundant_interpolation.rb b/lib/rubocop/cop/style/redundant_interpolation.rb index 823183f4f97..7ce40fab267 100644 --- a/lib/rubocop/cop/style/redundant_interpolation.rb +++ b/lib/rubocop/cop/style/redundant_interpolation.rb @@ -56,7 +56,7 @@ def single_variable_interpolation?(node) first_child = node.children.first variable_interpolation?(first_child) || - first_child.send_type? && !first_child.operator_method? + (first_child.send_type? && !first_child.operator_method?) end def interpolation?(node) diff --git a/lib/rubocop/cop/style/redundant_percent_q.rb b/lib/rubocop/cop/style/redundant_percent_q.rb index ae51355c54b..9a6ed9d2097 100644 --- a/lib/rubocop/cop/style/redundant_percent_q.rb +++ b/lib/rubocop/cop/style/redundant_percent_q.rb @@ -65,9 +65,8 @@ def interpolated_quotes?(node) end def allowed_percent_q?(node) - node.source.start_with?(PERCENT_Q) && acceptable_q?(node) || - node.source.start_with?(PERCENT_CAPITAL_Q) && - acceptable_capital_q?(node) + (node.source.start_with?(PERCENT_Q) && acceptable_q?(node)) || + (node.source.start_with?(PERCENT_CAPITAL_Q) && acceptable_capital_q?(node)) end def message(node) diff --git a/lib/rubocop/cop/style/redundant_self_assignment_branch.rb b/lib/rubocop/cop/style/redundant_self_assignment_branch.rb index a6cfa356360..7c6b841da18 100644 --- a/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +++ b/lib/rubocop/cop/style/redundant_self_assignment_branch.rb @@ -58,7 +58,7 @@ def use_if_and_else_branch?(expression) def inconvertible_to_modifier?(if_branch, else_branch) multiple_statements?(if_branch) || multiple_statements?(else_branch) || - else_branch.respond_to?(:elsif?) && else_branch.elsif? + (else_branch.respond_to?(:elsif?) && else_branch.elsif?) end def multiple_statements?(branch) diff --git a/lib/rubocop/cop/style/regexp_literal.rb b/lib/rubocop/cop/style/regexp_literal.rb index 13bd1bb06ac..107af5025e6 100644 --- a/lib/rubocop/cop/style/regexp_literal.rb +++ b/lib/rubocop/cop/style/regexp_literal.rb @@ -107,7 +107,7 @@ def on_regexp(node) private def allowed_slash_literal?(node) - style == :slashes && !contains_disallowed_slash?(node) || allowed_mixed_slash?(node) + (style == :slashes && !contains_disallowed_slash?(node)) || allowed_mixed_slash?(node) end def allowed_mixed_slash?(node) @@ -115,13 +115,13 @@ def allowed_mixed_slash?(node) end def allowed_percent_r_literal?(node) - style == :slashes && contains_disallowed_slash?(node) || + (style == :slashes && contains_disallowed_slash?(node)) || style == :percent_r || allowed_mixed_percent_r?(node) || allowed_omit_parentheses_with_percent_r_literal?(node) end def allowed_mixed_percent_r?(node) - style == :mixed && node.multiline? || contains_disallowed_slash?(node) + (style == :mixed && node.multiline?) || contains_disallowed_slash?(node) end def contains_disallowed_slash?(node) diff --git a/lib/rubocop/cop/style/return_nil.rb b/lib/rubocop/cop/style/return_nil.rb index 3a941e15e14..4ae35f545c0 100644 --- a/lib/rubocop/cop/style/return_nil.rb +++ b/lib/rubocop/cop/style/return_nil.rb @@ -74,7 +74,8 @@ def message(_node) end def correct_style?(node) - style == :return && !return_nil_node?(node) || style == :return_nil && !return_node?(node) + (style == :return && !return_nil_node?(node)) || + (style == :return_nil && !return_node?(node)) end def scoped_node?(node) diff --git a/lib/rubocop/cop/style/static_class.rb b/lib/rubocop/cop/style/static_class.rb index 946f22de6f7..5072a0140b0 100644 --- a/lib/rubocop/cop/style/static_class.rb +++ b/lib/rubocop/cop/style/static_class.rb @@ -60,8 +60,7 @@ def class_convertible_to_module?(class_node) return false if nodes.empty? nodes.all? do |node| - node_visibility(node) == :public && - node.defs_type? || + (node_visibility(node) == :public && node.defs_type?) || sclass_convertible_to_module?(node) || node.equals_asgn? || extend_call?(node) diff --git a/lib/rubocop/cop/style/string_concatenation.rb b/lib/rubocop/cop/style/string_concatenation.rb index 3ebca6b344c..847c2c6382b 100644 --- a/lib/rubocop/cop/style/string_concatenation.rb +++ b/lib/rubocop/cop/style/string_concatenation.rb @@ -93,7 +93,7 @@ def register_offense(topmost_plus_node, parts) def offensive_for_mode?(receiver_node) mode = cop_config['Mode'].to_sym - mode == :aggressive || mode == :conservative && receiver_node.str_type? + mode == :aggressive || (mode == :conservative && receiver_node.str_type?) end def line_end_concatenation?(node) diff --git a/lib/rubocop/cop/style/trivial_accessors.rb b/lib/rubocop/cop/style/trivial_accessors.rb index 14cdd49f553..54a35b82746 100644 --- a/lib/rubocop/cop/style/trivial_accessors.rb +++ b/lib/rubocop/cop/style/trivial_accessors.rb @@ -192,7 +192,7 @@ def trivial_writer?(node) def allowed_method_name?(node) allowed_method_names.include?(node.method_name) || - exact_name_match? && !names_match?(node) + (exact_name_match? && !names_match?(node)) end def allowed_writer?(method_name) diff --git a/lib/rubocop/cop/style/yoda_condition.rb b/lib/rubocop/cop/style/yoda_condition.rb index 012c6db2eb6..8db3c56a118 100644 --- a/lib/rubocop/cop/style/yoda_condition.rb +++ b/lib/rubocop/cop/style/yoda_condition.rb @@ -74,7 +74,7 @@ class YodaCondition < Base def on_send(node) return unless yoda_compatible_condition?(node) - return if equality_only? && non_equality_operator?(node) || + return if (equality_only? && non_equality_operator?(node)) || file_constant_equal_program_name?(node) valid_yoda?(node) || add_offense(node) do |corrector| @@ -102,8 +102,8 @@ def valid_yoda?(node) lhs = node.receiver rhs = node.first_argument - return true if lhs.literal? && rhs.literal? || - !lhs.literal? && !rhs.literal? || + return true if (lhs.literal? && rhs.literal?) || + (!lhs.literal? && !rhs.literal?) || interpolation?(lhs) enforce_yoda? ? lhs.literal? : rhs.literal? diff --git a/lib/rubocop/result_cache.rb b/lib/rubocop/result_cache.rb index 96daddf452d..568b53683bd 100644 --- a/lib/rubocop/result_cache.rb +++ b/lib/rubocop/result_cache.rb @@ -45,7 +45,7 @@ def requires_file_removal?(file_count, config_store) def remove_oldest_files(files, dirs, cache_root, verbose) # Add 1 to half the number of files, so that we remove the file if # there's only 1 left. - remove_count = 1 + files.length / 2 + remove_count = 1 + (files.length / 2) puts "Removing the #{remove_count} oldest files from #{cache_root}" if verbose sorted = files.sort_by { |path| File.mtime(path) } remove_files(sorted, dirs, remove_count) diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index e62889e8a94..2bf6bb44960 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -216,8 +216,7 @@ def file_finished(file, offenses) def cached_run? @cached_run ||= (@options[:cache] == 'true' || - @options[:cache] != 'false' && - @config_store.for_pwd.for_all_cops['UseCache']) && + (@options[:cache] != 'false' && @config_store.for_pwd.for_all_cops['UseCache'])) && # When running --auto-gen-config, there's some processing done in the # cops related to calculating the Max parameters for Metrics cops. We # need to do that processing and cannot use caching. diff --git a/spec/rubocop/formatter/progress_formatter_spec.rb b/spec/rubocop/formatter/progress_formatter_spec.rb index d9935d83f16..cf0db289d86 100644 --- a/spec/rubocop/formatter/progress_formatter_spec.rb +++ b/spec/rubocop/formatter/progress_formatter_spec.rb @@ -118,8 +118,8 @@ def offense_with_severity(severity) RuboCop::Cop::Offense.new( :error, Parser::Source::Range.new(source_buffer, - 4 * line_length + 1, - 4 * line_length + 2), + (4 * line_length) + 1, + (4 * line_length) + 2), 'bar', 'Cop' ), @@ -127,7 +127,7 @@ def offense_with_severity(severity) :convention, Parser::Source::Range.new(source_buffer, 5 * line_length, - 5 * line_length + 1), + (5 * line_length) + 1), 'foo', 'Cop' ) diff --git a/spec/rubocop/formatter/tap_formatter_spec.rb b/spec/rubocop/formatter/tap_formatter_spec.rb index 90e02998b03..ab3c59b1235 100644 --- a/spec/rubocop/formatter/tap_formatter_spec.rb +++ b/spec/rubocop/formatter/tap_formatter_spec.rb @@ -86,8 +86,8 @@ RuboCop::Cop::Offense.new( :error, Parser::Source::Range.new(source_buffer, - 4 * line_length + 1, - 4 * line_length + 2), + (4 * line_length) + 1, + (4 * line_length) + 2), 'bar', 'Cop' ), @@ -95,7 +95,7 @@ :convention, Parser::Source::Range.new(source_buffer, 5 * line_length, - 5 * line_length + 1), + (5 * line_length) + 1), 'foo', 'Cop' )