Skip to content

Commit

Permalink
Fix false positives for Style/OperatorMethodCall with named forwarding
Browse files Browse the repository at this point in the history
These cases are syntax errors as well, in addition to anonymous forwarding
  • Loading branch information
Earlopain committed Sep 11, 2024
1 parent 66d987a commit 135dad1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positives_for_operator_method_call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13224](https://github.com/rubocop/rubocop/pull/13224): Fix false positives for `Style/OperatorMethodCall` with named forwarding. ([@earlopain][])
12 changes: 7 additions & 5 deletions lib/rubocop/cop/style/operator_method_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ class OperatorMethodCall < Base

MSG = 'Redundant dot detected.'
RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ ! != !~].freeze
DISALLOWED_ARG_TYPES = %i[
splat kwsplat forwarded_args forwarded_restarg forwarded_kwrestarg block_pass
].freeze

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
def on_send(node)
return unless (dot = node.loc.dot)
return if node.receiver.const_type? || !node.arguments.one?

_lhs, _op, rhs = *node
return if !rhs || method_call_with_parenthesized_arg?(rhs) || anonymous_forwarding?(rhs)
return if !rhs || method_call_with_parenthesized_arg?(rhs) || forwarding?(rhs)

add_offense(dot) do |corrector|
wrap_in_parentheses_if_chained(corrector, node)
Expand All @@ -50,11 +53,10 @@ def method_call_with_parenthesized_arg?(argument)
argument.children.first && argument.parent.parenthesized?
end

def anonymous_forwarding?(argument)
return true if argument.forwarded_args_type? || argument.forwarded_restarg_type?
return true if argument.hash_type? && argument.children.first&.forwarded_kwrestarg_type?
def forwarding?(argument)
return DISALLOWED_ARG_TYPES.include?(argument.children.first&.type) if argument.hash_type?

argument.block_pass_type? && argument.source == '&'
DISALLOWED_ARG_TYPES.include?(argument.type)
end

def wrap_in_parentheses_if_chained(corrector, node)
Expand Down
69 changes: 24 additions & 45 deletions spec/rubocop/cop/style/operator_method_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,51 +75,6 @@
RUBY
end

it 'registers an offense when using named block forwarding' do
expect_offense(<<~RUBY)
def foo(&block)
bar.#{operator_method}(&block)
^ Redundant dot detected.
end
RUBY

expect_correction(<<~RUBY)
def foo(&block)
bar #{operator_method}(&block)
end
RUBY
end

it 'registers an offense when using named rest arguments forwarding' do
expect_offense(<<~RUBY)
def foo(*args)
bar.#{operator_method}(*args)
^ Redundant dot detected.
end
RUBY

expect_correction(<<~RUBY)
def foo(*args)
bar #{operator_method}(*args)
end
RUBY
end

it 'registers an offense when using named keyword rest arguments forwarding' do
expect_offense(<<~RUBY)
def foo(**options)
bar.#{operator_method}(**options)
^ Redundant dot detected.
end
RUBY

expect_correction(<<~RUBY)
def foo(**options)
bar #{operator_method}(**options)
end
RUBY
end

it 'does not register an offense when using multiple arguments' do
expect_no_offenses(<<~RUBY)
foo.#{operator_method}(bar, baz)
Expand Down Expand Up @@ -205,6 +160,14 @@ def foo(...)
RUBY
end

it 'does not register an offense when using named block forwarding' do
expect_no_offenses(<<~RUBY)
def foo(&blk)
bar.==(&blk)
end
RUBY
end

it 'does not register an offense when using anonymous block forwarding', :ruby31 do
expect_no_offenses(<<~RUBY)
def foo(&)
Expand All @@ -213,6 +176,14 @@ def foo(&)
RUBY
end

it 'does not register an offense when using named rest arguments forwarding' do
expect_no_offenses(<<~RUBY)
def foo(*args)
bar.==(*args)
end
RUBY
end

it 'does not register an offense when using anonymous rest arguments forwarding', :ruby32 do
expect_no_offenses(<<~RUBY)
def foo(*)
Expand All @@ -221,6 +192,14 @@ def foo(*)
RUBY
end

it 'does not register an offense when named anonymous keyword rest arguments forwarding' do
expect_no_offenses(<<~RUBY)
def foo(**kwargs)
bar.==(**kwargs)
end
RUBY
end

it 'does not register an offense when using anonymous keyword rest arguments forwarding', :ruby32 do
expect_no_offenses(<<~RUBY)
def foo(**)
Expand Down

0 comments on commit 135dad1

Please sign in to comment.