Skip to content

Commit

Permalink
Correct Lint/AmbiguousOperatorPrecedence offenses.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored and bbatsov committed Aug 29, 2021
1 parent 243095a commit 9885eef
Show file tree
Hide file tree
Showing 54 changed files with 86 additions and 86 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/cop/base.rb
Expand Up @@ -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)
Expand Down
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/line_break_corrector.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/argument_alignment.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/layout/class_structure.rb
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/end_alignment.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/leading_comment_space.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/line_length.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/multiline_block_layout.rb
Expand Up @@ -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

Expand Down
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/space_around_keyword.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_inside_reference_brackets.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/ambiguous_range.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/empty_in_pattern.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/erb_new_arguments.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/float_out_of_range.rb
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/rubocop/cop/lint/unused_method_argument.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/useless_times.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/metrics/perceived_complexity.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/metrics/utils/abc_size_calculator.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/metrics/utils/code_length_calculator.rb
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/code_length.rb
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/naming/constant_name.rb
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/accessor_grouping.rb
Expand Up @@ -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|
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/collection_methods.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/document_dynamic_eval_definition.rb
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/style/documentation.rb
Expand Up @@ -112,12 +112,12 @@ def check(node, body)

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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/empty_method.rb
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/if_with_boolean_literal_branches.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/lambda_call.rb
Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/mutable_constant.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/negated_if.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/negated_unless.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/non_nil_check.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9885eef

Please sign in to comment.