rust/tests/ui/min_max.rs

76 lines
2.1 KiB
Rust
Raw Normal View History

2018-07-28 10:34:52 -05:00
#![warn(clippy::all)]
2022-09-15 21:50:28 -05:00
#![allow(clippy::manual_clamp)]
2015-09-05 05:46:34 -05:00
use std::cmp::{max as my_max, max, min as my_min, min};
2015-09-05 05:46:34 -05:00
2018-12-09 16:26:16 -06:00
const LARGE: usize = 3;
2015-09-05 09:24:41 -05:00
2020-08-07 10:55:25 -05:00
struct NotOrd(u64);
impl NotOrd {
fn min(self, x: u64) -> NotOrd {
NotOrd(x)
}
fn max(self, x: u64) -> NotOrd {
NotOrd(x)
}
}
2015-09-05 05:46:34 -05:00
fn main() {
2021-11-18 07:54:49 -06:00
let x = 2usize;
2017-02-08 07:58:07 -06:00
min(1, max(3, x));
//~^ ERROR: this `min`/`max` combination leads to constant result
//~| NOTE: `-D clippy::min-max` implied by `-D warnings`
2017-02-08 07:58:07 -06:00
min(max(3, x), 1);
//~^ ERROR: this `min`/`max` combination leads to constant result
2017-02-08 07:58:07 -06:00
max(min(x, 1), 3);
//~^ ERROR: this `min`/`max` combination leads to constant result
2017-02-08 07:58:07 -06:00
max(3, min(x, 1));
//~^ ERROR: this `min`/`max` combination leads to constant result
2015-09-05 05:46:34 -05:00
2017-02-08 07:58:07 -06:00
my_max(3, my_min(x, 1));
//~^ ERROR: this `min`/`max` combination leads to constant result
2015-09-05 05:46:34 -05:00
min(3, max(1, x)); // ok, could be 1, 2 or 3 depending on x
2015-09-05 09:24:41 -05:00
min(1, max(LARGE, x)); // no error, we don't lookup consts here
2018-06-16 11:33:11 -05:00
let y = 2isize;
min(max(y, -1), 3);
2021-11-18 07:54:49 -06:00
let s = "Hello";
2017-02-08 07:58:07 -06:00
min("Apple", max("Zoo", s));
//~^ ERROR: this `min`/`max` combination leads to constant result
2017-02-08 07:58:07 -06:00
max(min(s, "Apple"), "Zoo");
//~^ ERROR: this `min`/`max` combination leads to constant result
2015-09-05 05:46:34 -05:00
max("Apple", min(s, "Zoo")); // ok
2020-08-07 10:55:25 -05:00
let f = 3f32;
x.min(1).max(3);
//~^ ERROR: this `min`/`max` combination leads to constant result
x.max(3).min(1);
//~^ ERROR: this `min`/`max` combination leads to constant result
2020-08-07 10:55:25 -05:00
f.max(3f32).min(1f32);
//~^ ERROR: this `min`/`max` combination leads to constant result
x.max(1).min(3); // ok
x.min(3).max(1); // ok
2020-08-07 10:55:25 -05:00
f.min(3f32).max(1f32); // ok
max(x.min(1), 3);
//~^ ERROR: this `min`/`max` combination leads to constant result
min(x.max(1), 3); // ok
s.max("Zoo").min("Apple");
//~^ ERROR: this `min`/`max` combination leads to constant result
s.min("Apple").max("Zoo");
//~^ ERROR: this `min`/`max` combination leads to constant result
s.min("Zoo").max("Apple"); // ok
2020-08-07 10:55:25 -05:00
let not_ord = NotOrd(1);
not_ord.min(1).max(3); // ok
2015-09-05 05:46:34 -05:00
}