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

.min(x).max(y) with x < y #5854

Closed
JarredAllen opened this issue Jul 29, 2020 · 3 comments · Fixed by #5871
Closed

.min(x).max(y) with x < y #5854

JarredAllen opened this issue Jul 29, 2020 · 3 comments · Fixed by #5871
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@JarredAllen
Copy link
Contributor

What it does

Lints against people using min and max methods (on the float types or on Ord) to constrain a value to a range, but getting the arguments to min and max backwards (such that the argument to min is less than the argument to max). In that case, instead of clamping the value to be between the two arguments (likely what was intended), it will always return the argument of the second function called.

Categories (optional)

  • Kind: correctness

Drawbacks

None. If someone wanted the result of the expression this lint triggers on (which is unlikely), they could just include the returned value directly.

Example

Given a floating-point variable x:

x.min(-1.0).max(1.0)

will always give the value 1.0, whereas

x.max(-1.0).min(1.0)

will give the value of x, clamped between -1.0 and 1.0, with 1.0 for NaN.

@JarredAllen JarredAllen added the A-lint Area: New lints label Jul 29, 2020
@flip1995 flip1995 added L-correctness Lint: Belongs in the correctness lint group L-suggestion Lint: Improving, adding or fixing lint suggestions good-first-issue These issues are a good way to get started with Clippy labels Jul 30, 2020
@flip1995
Copy link
Member

Should the suggestion be to swap the min and max functions or to swap their arguments (i.e. -1.0/1.0)? I'm leaning towards the latter.

@JarredAllen
Copy link
Contributor Author

The two options are equivalent, behavior-wise, except when working with floats if NaN is passed in (calling either function on NaN returns the argument, so the first parameter will be the result in that case). It might be better to keep the arguments and swap the functions, in case the code was written with which value NaN becomes in mind, though I don't know if that's a common enough occurrence to worry about. I've never written or seen code which clamps a float which might be NaN and cares about what value it becomes, but I wouldn't be surprised if it exists.

@flip1995
Copy link
Member

I would go for changing the order of the functions in that case. That way, we can guarantee to always emit a semantic equivalent suggestion.

wiomoc added a commit to wiomoc/rust-clippy that referenced this issue Aug 6, 2020
flip1995 added a commit to flip1995/rust-clippy that referenced this issue Aug 10, 2020
…flip1995

Lint .min(x).max(y) with x < y

Fixes  rust-lang#5854

changelog: Also lint `ord.min(a).max(b)`, where `a < b` in [`min_max`] lint
@bors bors closed this as completed in 0abc483 Aug 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants