If I have a local variable in scope with the same name as a method I want to call, I have two options:
self.foo instead of foofoo() instead of fooBoth 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
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
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
0.49.1
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.