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

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

Merged
merged 2 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions clippy_lints/src/minmax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ declare_clippy_lint! {
/// ```ignore
/// min(0, max(100, x))
/// ```
/// or
/// ```ignore
/// x.max(100).min(0)
/// ```
/// It will always be equal to `0`. Probably the author meant to clamp the value
/// between 0 and 100, but has erroneously swapped `min` and `max`.
pub MIN_MAX,
Expand Down Expand Up @@ -60,25 +64,35 @@ enum MinMax {
}

fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Constant, &'a Expr<'a>)> {
if let ExprKind::Call(ref path, ref args) = expr.kind {
if let ExprKind::Path(ref qpath) = path.kind {
cx.typeck_results()
.qpath_res(qpath, path.hir_id)
.opt_def_id()
.and_then(|def_id| {
if match_def_path(cx, def_id, &paths::CMP_MIN) {
fetch_const(cx, args, MinMax::Min)
} else if match_def_path(cx, def_id, &paths::CMP_MAX) {
fetch_const(cx, args, MinMax::Max)
} else {
None
}
})
} else {
None
}
} else {
None
match expr.kind {
ExprKind::Call(ref path, ref args) => {
if let ExprKind::Path(ref qpath) = path.kind {
cx.typeck_results()
.qpath_res(qpath, path.hir_id)
.opt_def_id()
.and_then(|def_id| {
if match_def_path(cx, def_id, &paths::CMP_MIN) {
fetch_const(cx, args, MinMax::Min)
} else if match_def_path(cx, def_id, &paths::CMP_MAX) {
fetch_const(cx, args, MinMax::Max)
} else {
None
}
})
} else {
None
}
},
ExprKind::MethodCall(ref path, _, ref args, _) => {
if path.ident.as_str() == sym!(max).as_str() {
fetch_const(cx, args, MinMax::Max)
} else if path.ident.as_str() == sym!(min).as_str() {
fetch_const(cx, args, MinMax::Min)
} else {
None
}
wiomoc marked this conversation as resolved.
Show resolved Hide resolved
},
_ => None,
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/ui/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ fn main() {
max(min(s, "Apple"), "Zoo");

max("Apple", min(s, "Zoo")); // ok

x.min(1).max(3);
x.max(3).min(1);

x.max(1).min(3); // ok
x.min(3).max(1); // ok

max(x.min(1), 3);
min(x.max(1), 3); // ok

s.max("Zoo").min("Apple");
s.min("Apple").max("Zoo");

s.min("Zoo").max("Apple"); // ok
}
32 changes: 31 additions & 1 deletion tests/ui/min_max.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,35 @@ error: this `min`/`max` combination leads to constant result
LL | max(min(s, "Apple"), "Zoo");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors
error: this `min`/`max` combination leads to constant result
--> $DIR/min_max.rs:34:5
|
LL | x.min(1).max(3);
| ^^^^^^^^^^^^^^^

error: this `min`/`max` combination leads to constant result
--> $DIR/min_max.rs:35:5
|
LL | x.max(3).min(1);
| ^^^^^^^^^^^^^^^

error: this `min`/`max` combination leads to constant result
--> $DIR/min_max.rs:40:5
|
LL | max(x.min(1), 3);
| ^^^^^^^^^^^^^^^^

error: this `min`/`max` combination leads to constant result
--> $DIR/min_max.rs:43:5
|
LL | s.max("Zoo").min("Apple");
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: this `min`/`max` combination leads to constant result
--> $DIR/min_max.rs:44:5
|
LL | s.min("Apple").max("Zoo");
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 12 previous errors