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 #8782] Mark any condition with defined? as an unsafe autocorrection if it is not parenthesized #8797

Merged

Conversation

dvandersluis
Copy link
Member

Previously, the check for defined? in unsafe_autocorrect? only applied when the argument was a single send node with no receiver, but that meant that most times defined? was used in a ternary wrapped in parens, it would be autocorrected incorrectly (since defined? would then be called on the entire ternary instead of the argument). This change makes unsafe_autocorrect? return true if there is any defined? call without parentheses.

Fixes #8782.


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.

@dvandersluis dvandersluis force-pushed the style-ternary-parens-autocorrect branch 6 times, most recently from 34e0f8a to 4543f53 Compare October 3, 2020 18:19
@dvandersluis dvandersluis force-pushed the style-ternary-parens-autocorrect branch from 4543f53 to ad6d44f Compare October 8, 2020 16:25
@dvandersluis
Copy link
Member Author

@marcandre @bbatsov would you be able to take a look at this one too?

Copy link
Collaborator

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appart from my small remark the changes look good. Sorry about the slow response!

…correction if it is not parenthesized.

Without parens, `defined?` will apply to the entire ternary instead of its previous argument, which makes this autocorrection incorrect.
@dvandersluis dvandersluis force-pushed the style-ternary-parens-autocorrect branch from ad6d44f to dd6944c Compare October 8, 2020 20:34
@dvandersluis
Copy link
Member Author

@bbatsov no worries, updated with your requested changes now!

@marcandre marcandre merged commit 87f5d45 into rubocop:master Oct 8, 2020
@marcandre
Copy link
Contributor

Thanks!

@dvandersluis dvandersluis deleted the style-ternary-parens-autocorrect branch January 18, 2021 20:42
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.

Style/TernaryParentheses still not necessarily safe autocorrect for defined? keyword
3 participants