Skip to content

Commit ee8db50

Browse files
authored
Rollup merge of rust-lang#5871 - wiomoc:feature/methodcall-minmax, r=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
2 parents 08ab29b + 87e7409 commit ee8db50

File tree

3 files changed

+115
-24
lines changed

3 files changed

+115
-24
lines changed

clippy_lints/src/minmax.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::consts::{constant_simple, Constant};
2-
use crate::utils::{match_def_path, paths, span_lint};
2+
use crate::utils::{match_def_path, match_trait_method, paths, span_lint};
3+
use if_chain::if_chain;
34
use rustc_hir::{Expr, ExprKind};
45
use rustc_lint::{LateContext, LateLintPass};
56
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -18,6 +19,10 @@ declare_clippy_lint! {
1819
/// ```ignore
1920
/// min(0, max(100, x))
2021
/// ```
22+
/// or
23+
/// ```ignore
24+
/// x.max(100).min(0)
25+
/// ```
2126
/// It will always be equal to `0`. Probably the author meant to clamp the value
2227
/// between 0 and 100, but has erroneously swapped `min` and `max`.
2328
pub MIN_MAX,
@@ -60,25 +65,43 @@ enum MinMax {
6065
}
6166

6267
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) {
68+
match expr.kind {
69+
ExprKind::Call(ref path, ref args) => {
70+
if let ExprKind::Path(ref qpath) = path.kind {
71+
cx.typeck_results()
72+
.qpath_res(qpath, path.hir_id)
73+
.opt_def_id()
74+
.and_then(|def_id| {
75+
if match_def_path(cx, def_id, &paths::CMP_MIN) {
76+
fetch_const(cx, args, MinMax::Min)
77+
} else if match_def_path(cx, def_id, &paths::CMP_MAX) {
78+
fetch_const(cx, args, MinMax::Max)
79+
} else {
80+
None
81+
}
82+
})
83+
} else {
84+
None
85+
}
86+
},
87+
ExprKind::MethodCall(ref path, _, ref args, _) => {
88+
if_chain! {
89+
if let [obj, _] = args;
90+
if cx.typeck_results().expr_ty(obj).is_floating_point() || match_trait_method(cx, expr, &paths::ORD);
91+
then {
92+
if path.ident.as_str() == sym!(max).as_str() {
7293
fetch_const(cx, args, MinMax::Max)
94+
} else if path.ident.as_str() == sym!(min).as_str() {
95+
fetch_const(cx, args, MinMax::Min)
7396
} else {
7497
None
7598
}
76-
})
77-
} else {
78-
None
79-
}
80-
} else {
81-
None
99+
} else {
100+
None
101+
}
102+
}
103+
},
104+
_ => None,
82105
}
83106
}
84107

tests/ui/min_max.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ use std::cmp::{max, min};
66

77
const LARGE: usize = 3;
88

9+
struct NotOrd(u64);
10+
11+
impl NotOrd {
12+
fn min(self, x: u64) -> NotOrd {
13+
NotOrd(x)
14+
}
15+
16+
fn max(self, x: u64) -> NotOrd {
17+
NotOrd(x)
18+
}
19+
}
20+
921
fn main() {
1022
let x;
1123
x = 2usize;
@@ -30,4 +42,24 @@ fn main() {
3042
max(min(s, "Apple"), "Zoo");
3143

3244
max("Apple", min(s, "Zoo")); // ok
45+
46+
let f = 3f32;
47+
x.min(1).max(3);
48+
x.max(3).min(1);
49+
f.max(3f32).min(1f32);
50+
51+
x.max(1).min(3); // ok
52+
x.min(3).max(1); // ok
53+
f.min(3f32).max(1f32); // ok
54+
55+
max(x.min(1), 3);
56+
min(x.max(1), 3); // ok
57+
58+
s.max("Zoo").min("Apple");
59+
s.min("Apple").max("Zoo");
60+
61+
s.min("Zoo").max("Apple"); // ok
62+
63+
let not_ord = NotOrd(1);
64+
not_ord.min(1).max(3); // ok
3365
}

tests/ui/min_max.stderr

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,82 @@
11
error: this `min`/`max` combination leads to constant result
2-
--> $DIR/min_max.rs:12:5
2+
--> $DIR/min_max.rs:24:5
33
|
44
LL | min(1, max(3, x));
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::min-max` implied by `-D warnings`
88

99
error: this `min`/`max` combination leads to constant result
10-
--> $DIR/min_max.rs:13:5
10+
--> $DIR/min_max.rs:25:5
1111
|
1212
LL | min(max(3, x), 1);
1313
| ^^^^^^^^^^^^^^^^^
1414

1515
error: this `min`/`max` combination leads to constant result
16-
--> $DIR/min_max.rs:14:5
16+
--> $DIR/min_max.rs:26:5
1717
|
1818
LL | max(min(x, 1), 3);
1919
| ^^^^^^^^^^^^^^^^^
2020

2121
error: this `min`/`max` combination leads to constant result
22-
--> $DIR/min_max.rs:15:5
22+
--> $DIR/min_max.rs:27:5
2323
|
2424
LL | max(3, min(x, 1));
2525
| ^^^^^^^^^^^^^^^^^
2626

2727
error: this `min`/`max` combination leads to constant result
28-
--> $DIR/min_max.rs:17:5
28+
--> $DIR/min_max.rs:29:5
2929
|
3030
LL | my_max(3, my_min(x, 1));
3131
| ^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
error: this `min`/`max` combination leads to constant result
34-
--> $DIR/min_max.rs:29:5
34+
--> $DIR/min_max.rs:41:5
3535
|
3636
LL | min("Apple", max("Zoo", s));
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: this `min`/`max` combination leads to constant result
40-
--> $DIR/min_max.rs:30:5
40+
--> $DIR/min_max.rs:42:5
4141
|
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:47: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:48: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:49:5
59+
|
60+
LL | f.max(3f32).min(1f32);
61+
| ^^^^^^^^^^^^^^^^^^^^^
62+
63+
error: this `min`/`max` combination leads to constant result
64+
--> $DIR/min_max.rs:55:5
65+
|
66+
LL | max(x.min(1), 3);
67+
| ^^^^^^^^^^^^^^^^
68+
69+
error: this `min`/`max` combination leads to constant result
70+
--> $DIR/min_max.rs:58:5
71+
|
72+
LL | s.max("Zoo").min("Apple");
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
74+
75+
error: this `min`/`max` combination leads to constant result
76+
--> $DIR/min_max.rs:59:5
77+
|
78+
LL | s.min("Apple").max("Zoo");
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
80+
81+
error: aborting due to 13 previous errors
4682

0 commit comments

Comments
 (0)