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 false positive in Style/RedundantSelf cop with nested self access #8585

Merged

Conversation

marcotc
Copy link
Contributor

@marcotc marcotc commented Aug 25, 2020

This PR fixes the following false positive:

a = self.a || b

which currently returns:

file.rb:1:5: C: Style/RedundantSelf: Redundant self detected.
a = self.a || b
    ^^^^^^

This is a false positive, as self.a cannot be changed to a while keeping the same semantics. Changing it to a would effectively make the left-hand side of this or statement always nil, as a has just been declared and still doesn't have a value assigned.

This happens because the logic on redundant_self.rb does not check for nested statements (in the example, self.a is nested node inside an or node).
This PR adds an ancestor search, starting from self.a, to try to find if any of its ancestors are associated with a local variable to name a. The changes introduced work for any level of nesting.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 14, 2020

The changes look good, but your branch has to be rebased on top of the current master branch due to merge conflicts.

@marcotc
Copy link
Contributor Author

marcotc commented Sep 14, 2020

Thank you, @bbatsov, a simple rebase did the trick. Let me know if there's any additional feedback!

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 17, 2020

@marcotc I totally dropped the ball on you. Sorry! Can you rebase again and move the changelog entry to the unreleased section?

@bbatsov bbatsov merged commit 89c9628 into rubocop:master Oct 21, 2020
@marcotc
Copy link
Contributor Author

marcotc commented Oct 27, 2020

Sorry for not rebasing earlier, @bbatsov, I was on vacation recently. Thank you very much on merging it!

Also, huge congratulations on shipping 1.0!

@marcotc marcotc deleted the fix/cop-redundant-self-on-nested-expression branch October 27, 2020 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants