Rubocop: False positive for MethodCallWithoutArgsParentheses

Created on 16 Jun 2017  路  5Comments  路  Source: rubocop-hq/rubocop

If I have a local variable in scope with the same name as a method I want to call, I have two options:

  1. Write self.foo instead of foo
  2. Write foo() instead of foo

Both clarify to ruby that this foo is a send node. The second option though isn't respected when there is a local variable in scope


Expected behavior

This code does not generate an offense with the cop in question

$ cat ex.rb
def foo
  puts 'Hiya'
end

bar = 1
foo = bar
result = foo()

$ rubocop --only MethodCallWithoutArgsParentheses ex.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Actual behavior

It tells me to remove the parenthesis but I want to write foo() and not self.foo

$ cat ex.rb
def foo
  puts 'Hiya'
end

bar = 1
foo = bar
result = foo()

$ rubocop --only MethodCallWithoutArgsParentheses ex.rb
Inspecting 1 file
C

Offenses:

ex.rb:7:13: C: Style/MethodCallWithoutArgsParentheses: Do not use parentheses for method calls with no arguments.
result = foo()
            ^

1 file inspected, 1 offense detected

RuboCop version

0.49.1

bug stale

All 5 comments

Should there also be a ShadowingInstanceMethod, like there's ShadowingOuterLocalVariable? 馃

As an additional note, this also has semantically incorrect autocorrect behavior:

Given that we have a method and lvar like so:

def correct
  true
end

correct = false

Before autocorrect:

puts "Prints true? #{correct || correct()}" # => Prints true? true

After autocorrect:

puts "Prints true? #{correct || correct}" # => Prints true? false

I imagine encountering it is semi-rare, but it happened to me recently. :(

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

This is not the most common code, but I believe the false positive is still valid (and the advice causes a semantic change!) and could be detected by examining the local variables in scope. @Drenmi's suggestion might make sense for most people, though every time I've actually done this it's been helpful--usually to avoid some name forced upon me by a dsl or to do some kind of local memoization without using an ivar.

Is there a local variable scope helper in rubocop? If there is something that can give you a list of what's in scope easily, this should be pretty easy to avoid.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Aqualon picture Aqualon  路  3Comments

Ana06 picture Ana06  路  3Comments

bquorning picture bquorning  路  3Comments

cabello picture cabello  路  3Comments

deivid-rodriguez picture deivid-rodriguez  路  3Comments