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

Add new Style/MinMaxComparison cop #11341

Merged
merged 1 commit into from Dec 31, 2022

Conversation

koic
Copy link
Member

@koic koic commented Dec 27, 2022

Implement part of #11338.

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.

# 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

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.


def on_if(node)
lhs, operator, rhs = *node.condition
return unless %i[> <].include?(operator)

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

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 koic force-pushed the add_new_style_min_max_comparison_cop branch 2 times, most recently from 780960f to 8427e36 Compare December 27, 2022 14:36
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 koic force-pushed the add_new_style_min_max_comparison_cop branch from 8427e36 to 8f334c0 Compare December 27, 2022 14:41
@bbatsov bbatsov merged commit f14e700 into rubocop:master Dec 31, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 31, 2022

Looks good to me. Thanks!

@koic koic deleted the add_new_style_min_max_comparison_cop branch December 31, 2022 07:19
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants