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

wrong detection of Style/RedundantSelf #9843

Closed
vitaly opened this issue Jun 1, 2021 · 0 comments · Fixed by #9847
Closed

wrong detection of Style/RedundantSelf #9843

vitaly opened this issue Jun 1, 2021 · 0 comments · Fixed by #9847
Labels

Comments

@vitaly
Copy link

vitaly commented Jun 1, 2021

Rubocop incorrectly detects a case of Style/RedundantSelf in the following example:

def foo
  bar = self.bar if self.bar
  bar
end

Expected behavior

Should not falsely detect Style/RedundantSelf in the provided example.

Actual behavior

When running with rubocop -a it "fixes" it to the following:

def foo
  bar = self.bar if bar
  bar
end

This is incorrect, as the value of bar in this case will always be nil regardless of self.bar

For /private/tmp/rubocop: Default configuration from /Users/vitaly/.asdf/installs/ruby/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-1.16.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/rubocop/bar.rb
C

Offenses:

bar.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
def foo
^
bar.rb:2:21: C: [Corrected] Style/RedundantSelf: Redundant self detected.
  bar = self.bar if self.bar
                    ^^^^^^^^

1 file inspected, 2 offenses detected, 1 offense corrected, 1 more offense can be corrected with `rubocop -A`
Finished in 0.12974799999210518 seconds

Steps to reproduce the problem

Create a file foo.rb with the following content:

def foo
  bar = self.bar if self.bar
  bar
end

Run rubocop -a foo.rb

It should not remove self after if

RuboCop version

$ rubocop -V
1.16.0 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 2.6.5 x86_64-darwin19)
@koic koic added the bug label Jun 1, 2021
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Jun 2, 2021
…es to use `self` in the condition when a variable named is shadowed inside.
@koic koic closed this as completed in #9847 Jun 2, 2021
koic added a commit that referenced this issue Jun 2, 2021
[Fix #9843] Fix `Style/RedundantSelf` to allow conditional nodes to use `self` in the condition when a variable named is shadowed inside
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants