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
Add new Style/MinMaxComparison
cop
#11341
Merged
bbatsov
merged 1 commit into
rubocop:master
from
koic:add_new_style_min_max_comparison_cop
Dec 31, 2022
Merged
Add new Style/MinMaxComparison
cop
#11341
bbatsov
merged 1 commit into
rubocop:master
from
koic:add_new_style_min_max_comparison_cop
Dec 31, 2022
Conversation
This file contains 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
|
||
def on_if(node) | ||
lhs, operator, rhs = *node.condition | ||
return unless %i[> <].include?(operator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this cop would also work with >=
and <=
# these can be both replaced with `[a, b].max`
a > b ? a : b
a >= b ? a : b
Comment on lines
17
to
19
# # bad | ||
# [a, b].max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be # good
😄
koic
force-pushed
the
add_new_style_min_max_comparison_cop
branch
2 times, most recently
from
December 27, 2022 14:36
780960f
to
8427e36
Compare
This PR adds new `Style/MinMaxComparison` cop. This cop Enforces the use of `max` or `min` instead of comparison for greater or less. It is unsafe because even if a value has `<` or `>` method, it is not necessarily `Comparable`. ```ruby # bad a > b ? a : b a >= b ? a : b # good [a, b].max # bad a < b ? a : b a <= b ? a : b # good [a, b].min ```
koic
force-pushed
the
add_new_style_min_max_comparison_cop
branch
from
December 27, 2022 14:41
8427e36
to
8f334c0
Compare
Looks good to me. Thanks! |
8 tasks
koic
added a commit
to koic/rubocop
that referenced
this pull request
Jan 14, 2023
## Summary Fixes rubocop#11338. This PR adds new `Style/ComparableClamp` cop. It enforces the use of `Comparable#clamp` instead of comparison by minimum and maximum. ```ruby # bad [[x, low].max, high].min # bad if x < low low elsif high < x high else x end # good x.clamp(low, high) ``` This cop supports autocorrection for `if/elsif/else` bad style only. Because `ArgumentError` occurs if the minimum and maximum of `clamp` arguments are reversed. When these are variables, it is not possible to determine which is the minimum and maximum: ```ruby [1, [2, 3].max].min # => 1 1.clamp(3, 1) # => min argument must be smaller than max argument (ArgumentError) ``` ## Additional Information - `Comparable#clamp` was introduced in Ruby 2.4 - Part of rubocop#11338 was implemented by rubocop#11341
bbatsov
pushed a commit
that referenced
this pull request
Jan 15, 2023
## Summary Fixes #11338. This PR adds new `Style/ComparableClamp` cop. It enforces the use of `Comparable#clamp` instead of comparison by minimum and maximum. ```ruby # bad [[x, low].max, high].min # bad if x < low low elsif high < x high else x end # good x.clamp(low, high) ``` This cop supports autocorrection for `if/elsif/else` bad style only. Because `ArgumentError` occurs if the minimum and maximum of `clamp` arguments are reversed. When these are variables, it is not possible to determine which is the minimum and maximum: ```ruby [1, [2, 3].max].min # => 1 1.clamp(3, 1) # => min argument must be smaller than max argument (ArgumentError) ``` ## Additional Information - `Comparable#clamp` was introduced in Ruby 2.4 - Part of #11338 was implemented by #11341
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.
Implement part of #11338.
This PR adds new
Style/MinMaxComparison
cop.This cop Enforces the use of
max
ormin
instead of comparison for greater or less. It is unsafe because even if a value has<
or>
method, it is not necessarilyComparable
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.