Skip to content

Commit 87e7409

Browse files
committed
check impl Ord / is_float
1 parent 0abc483 commit 87e7409

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

clippy_lints/src/minmax.rs

Lines changed: 16 additions & 7 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};
@@ -84,12 +85,20 @@ fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Cons
8485
}
8586
},
8687
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
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() {
93+
fetch_const(cx, args, MinMax::Max)
94+
} else if path.ident.as_str() == sym!(min).as_str() {
95+
fetch_const(cx, args, MinMax::Min)
96+
} else {
97+
None
98+
}
99+
} else {
100+
None
101+
}
93102
}
94103
},
95104
_ => None,

tests/ui/min_max.rs

Lines changed: 18 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;
@@ -31,11 +43,14 @@ fn main() {
3143

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

46+
let f = 3f32;
3447
x.min(1).max(3);
3548
x.max(3).min(1);
49+
f.max(3f32).min(1f32);
3650

3751
x.max(1).min(3); // ok
3852
x.min(3).max(1); // ok
53+
f.min(3f32).max(1f32); // ok
3954

4055
max(x.min(1), 3);
4156
min(x.max(1), 3); // ok
@@ -44,4 +59,7 @@ fn main() {
4459
s.min("Apple").max("Zoo");
4560

4661
s.min("Zoo").max("Apple"); // ok
62+
63+
let not_ord = NotOrd(1);
64+
not_ord.min(1).max(3); // ok
4765
}

tests/ui/min_max.stderr

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +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

4545
error: this `min`/`max` combination leads to constant result
46-
--> $DIR/min_max.rs:34:5
46+
--> $DIR/min_max.rs:47:5
4747
|
4848
LL | x.min(1).max(3);
4949
| ^^^^^^^^^^^^^^^
5050

5151
error: this `min`/`max` combination leads to constant result
52-
--> $DIR/min_max.rs:35:5
52+
--> $DIR/min_max.rs:48:5
5353
|
5454
LL | x.max(3).min(1);
5555
| ^^^^^^^^^^^^^^^
5656

5757
error: this `min`/`max` combination leads to constant result
58-
--> $DIR/min_max.rs:40:5
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
5965
|
6066
LL | max(x.min(1), 3);
6167
| ^^^^^^^^^^^^^^^^
6268

6369
error: this `min`/`max` combination leads to constant result
64-
--> $DIR/min_max.rs:43:5
70+
--> $DIR/min_max.rs:58:5
6571
|
6672
LL | s.max("Zoo").min("Apple");
6773
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6874

6975
error: this `min`/`max` combination leads to constant result
70-
--> $DIR/min_max.rs:44:5
76+
--> $DIR/min_max.rs:59:5
7177
|
7278
LL | s.min("Apple").max("Zoo");
7379
| ^^^^^^^^^^^^^^^^^^^^^^^^^
7480

75-
error: aborting due to 12 previous errors
81+
error: aborting due to 13 previous errors
7682

0 commit comments

Comments
 (0)