Fix incorrect autocorrect when Style/NumericPredicate is used with negations#12881
Merged
koic merged 1 commit intorubocop:masterfrom May 3, 2024
Merged
Conversation
koic
reviewed
May 3, 2024
|
|
||
| expect_correction(<<~RUBY) | ||
| number == 0 | ||
| (number == 0) |
Member
There was a problem hiding this comment.
Parentheses should not be added when it is not needed.
koic
reviewed
May 3, 2024
|
|
||
| expect_correction(<<~RUBY) | ||
| number > 0 | ||
| (number > 0) |
koic
reviewed
May 3, 2024
|
|
||
| expect_correction(<<~RUBY) | ||
| number < 0 | ||
| (number < 0) |
a8c58c5 to
28ce72d
Compare
koic
reviewed
May 3, 2024
Comment on lines
+163
to
+164
| parent = node.parent | ||
| parent&.send_type? && parent&.method?(:!) |
Member
There was a problem hiding this comment.
Can you refactor to the following?
Suggested change
| parent = node.parent | |
| parent&.send_type? && parent&.method?(:!) | |
| return false unless (parent = node.parent) | |
| parent.send_type? && parent.method?(:!) |
28ce72d to
bfbc4bf
Compare
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using
Style/NumericPredicatecop withEnforcedStyle: comparisonand having some negations, rubocop suggests invalid autocorrections.For example:
In this PR, I did the simplest change possible to just wrap the autocorrected code in the parentheses and it is assumed then after this other cops will convert it from
!(foo == 0)to something likefoo != 0. Which is actually the case.