Skip to content

Commit 0abc483

Browse files
committed
Lint .min(x).max(y) with x < y
Fixes #5854
1 parent 2d4c337 commit 0abc483

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

clippy_lints/src/minmax.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ declare_clippy_lint! {
1818
/// ```ignore
1919
/// min(0, max(100, x))
2020
/// ```
21+
/// or
22+
/// ```ignore
23+
/// x.max(100).min(0)
24+
/// ```
2125
/// It will always be equal to `0`. Probably the author meant to clamp the value
2226
/// between 0 and 100, but has erroneously swapped `min` and `max`.
2327
pub MIN_MAX,
@@ -60,25 +64,35 @@ enum MinMax {
6064
}
6165

6266
fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Constant, &'a Expr<'a>)> {
63-
if let ExprKind::Call(ref path, ref args) = expr.kind {
64-
if let ExprKind::Path(ref qpath) = path.kind {
65-
cx.typeck_results()
66-
.qpath_res(qpath, path.hir_id)
67-
.opt_def_id()
68-
.and_then(|def_id| {
69-
if match_def_path(cx, def_id, &paths::CMP_MIN) {
70-
fetch_const(cx, args, MinMax::Min)
71-
} else if match_def_path(cx, def_id, &paths::CMP_MAX) {
72-
fetch_const(cx, args, MinMax::Max)
73-
} else {
74-
None
75-
}
76-
})
77-
} else {
78-
None
79-
}
80-
} else {
81-
None
67+
match expr.kind {
68+
ExprKind::Call(ref path, ref args) => {
69+
if let ExprKind::Path(ref qpath) = path.kind {
70+
cx.typeck_results()
71+
.qpath_res(qpath, path.hir_id)
72+
.opt_def_id()
73+
.and_then(|def_id| {
74+
if match_def_path(cx, def_id, &paths::CMP_MIN) {
75+
fetch_const(cx, args, MinMax::Min)
76+
} else if match_def_path(cx, def_id, &paths::CMP_MAX) {
77+
fetch_const(cx, args, MinMax::Max)
78+
} else {
79+
None
80+
}
81+
})
82+
} else {
83+
None
84+
}
85+
},
86+
ExprKind::MethodCall(ref path, _, ref args, _) => {
87+
if path.ident.as_str() == sym!(max).as_str() {
88+
fetch_const(cx, args, MinMax::Max)
89+
} else if path.ident.as_str() == sym!(min).as_str() {
90+
fetch_const(cx, args, MinMax::Min)
91+
} else {
92+
None
93+
}
94+
},
95+
_ => None,
8296
}
8397
}
8498

tests/ui/min_max.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ fn main() {
3030
max(min(s, "Apple"), "Zoo");
3131

3232
max("Apple", min(s, "Zoo")); // ok
33+
34+
x.min(1).max(3);
35+
x.max(3).min(1);
36+
37+
x.max(1).min(3); // ok
38+
x.min(3).max(1); // ok
39+
40+
max(x.min(1), 3);
41+
min(x.max(1), 3); // ok
42+
43+
s.max("Zoo").min("Apple");
44+
s.min("Apple").max("Zoo");
45+
46+
s.min("Zoo").max("Apple"); // ok
3347
}

tests/ui/min_max.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,35 @@ error: this `min`/`max` combination leads to constant result
4242
LL | max(min(s, "Apple"), "Zoo");
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
error: aborting due to 7 previous errors
45+
error: this `min`/`max` combination leads to constant result
46+
--> $DIR/min_max.rs:34:5
47+
|
48+
LL | x.min(1).max(3);
49+
| ^^^^^^^^^^^^^^^
50+
51+
error: this `min`/`max` combination leads to constant result
52+
--> $DIR/min_max.rs:35:5
53+
|
54+
LL | x.max(3).min(1);
55+
| ^^^^^^^^^^^^^^^
56+
57+
error: this `min`/`max` combination leads to constant result
58+
--> $DIR/min_max.rs:40:5
59+
|
60+
LL | max(x.min(1), 3);
61+
| ^^^^^^^^^^^^^^^^
62+
63+
error: this `min`/`max` combination leads to constant result
64+
--> $DIR/min_max.rs:43:5
65+
|
66+
LL | s.max("Zoo").min("Apple");
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
68+
69+
error: this `min`/`max` combination leads to constant result
70+
--> $DIR/min_max.rs:44:5
71+
|
72+
LL | s.min("Apple").max("Zoo");
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
74+
75+
error: aborting due to 12 previous errors
4676

0 commit comments

Comments
 (0)