Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false negative for Style/OperatorMethodCall #11379

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11379](https://github.com/rubocop/rubocop/pull/11379): Fix a false negative for `Style/OperatorMethodCall` when using `a.+ b.something`. ([@koic][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/style/operator_method_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def on_send(node)
return if node.receiver.const_type?

_lhs, _op, rhs = *node
return if rhs.nil? || rhs.children.first || rhs.forwarded_args_type?
return if rhs.nil? || parenthesized_argument?(rhs) || rhs.forwarded_args_type?

add_offense(dot) do |corrector|
wrap_in_parentheses_if_chained(corrector, node)
Expand All @@ -38,6 +38,10 @@ def on_send(node)

private

def parenthesized_argument?(argument)
argument.children.first && argument.parent.parenthesized?
end

def wrap_in_parentheses_if_chained(corrector, node)
return unless node.parent&.call_type?

Expand Down
15 changes: 13 additions & 2 deletions spec/rubocop/cop/style/operator_method_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@
end
end

it 'does not register an offense when using `foo.+@bar.to_s`' do
it 'registers an offense when using `foo.+ @bar.to_s`' do
expect_offense(<<~RUBY)
foo.+ @bar.to_s
^ Redundant dot detected.
RUBY

expect_correction(<<~RUBY)
foo + @bar.to_s
RUBY
end

it 'does not register an offense when using `foo.+(@bar).to_s`' do
expect_no_offenses(<<~RUBY)
foo.+ bar.to_s
foo.+(@bar).to_s
RUBY
end

Expand Down