fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
8.5 KiB296 linesraw
1#![warn(clippy::manual_clamp)]
2#![allow(
3    unused,
4    dead_code,
5    clippy::unnecessary_operation,
6    clippy::no_effect,
7    clippy::if_same_then_else
8)]
9
10use std::cmp::{max as cmp_max, min as cmp_min};
11
12const CONST_MAX: i32 = 10;
13const CONST_MIN: i32 = 4;
14
15const CONST_F64_MAX: f64 = 10.0;
16const CONST_F64_MIN: f64 = 4.0;
17
18fn main() {
19    let (input, min, max) = (0, -2, 3);
20    // Lint
21    let x0 = input.clamp(min, max);
22
23    let x1 = input.clamp(min, max);
24
25    let x2 = input.clamp(min, max);
26
27    let x3 = input.clamp(min, max);
28
29    let x4 = input.clamp(min, max);
30    //~^ ERROR: clamp-like pattern without using clamp function
31    //~| NOTE: clamp will panic if max < min
32
33    let x5 = input.clamp(min, max);
34    //~^ ERROR: clamp-like pattern without using clamp function
35    //~| NOTE: clamp will panic if max < min
36
37    let x6 = input.clamp(min, max);
38
39    let x7 = input.clamp(min, max);
40
41    let x8 = input.clamp(min, max);
42
43    let mut x9 = input;
44    x9 = x9.clamp(min, max);
45
46    let x10 = input.clamp(min, max);
47
48    let mut x11 = input;
49    let _ = 1;
50    x11 = x11.clamp(min, max);
51
52    let mut x12 = input;
53    x12 = x12.clamp(min, max);
54
55    let mut x13 = input;
56    x13 = x13.clamp(min, max);
57
58    let x14 = input.clamp(CONST_MIN, CONST_MAX);
59    {
60        let (input, min, max) = (0.0f64, -2.0, 3.0);
61        let x15 = input.clamp(min, max);
62    }
63    {
64        let input: i32 = cmp_min_max(1);
65        // These can only be detected if exactly one of the arguments to the inner function is const.
66        let x16 = input.clamp(CONST_MIN, CONST_MAX);
67        //~^ ERROR: clamp-like pattern without using clamp function
68        //~| NOTE: clamp will panic if max < min
69        let x17 = input.clamp(CONST_MIN, CONST_MAX);
70        //~^ ERROR: clamp-like pattern without using clamp function
71        //~| NOTE: clamp will panic if max < min
72        let x18 = input.clamp(CONST_MIN, CONST_MAX);
73        //~^ ERROR: clamp-like pattern without using clamp function
74        //~| NOTE: clamp will panic if max < min
75        let x19 = input.clamp(CONST_MIN, CONST_MAX);
76        //~^ ERROR: clamp-like pattern without using clamp function
77        //~| NOTE: clamp will panic if max < min
78        let x20 = input.clamp(CONST_MIN, CONST_MAX);
79        //~^ ERROR: clamp-like pattern without using clamp function
80        //~| NOTE: clamp will panic if max < min
81        let x21 = input.clamp(CONST_MIN, CONST_MAX);
82        //~^ ERROR: clamp-like pattern without using clamp function
83        //~| NOTE: clamp will panic if max < min
84        let x22 = input.clamp(CONST_MIN, CONST_MAX);
85        //~^ ERROR: clamp-like pattern without using clamp function
86        //~| NOTE: clamp will panic if max < min
87        let x23 = input.clamp(CONST_MIN, CONST_MAX);
88        //~^ ERROR: clamp-like pattern without using clamp function
89        //~| NOTE: clamp will panic if max < min
90        let input: f64 = cmp_min_max(1) as f64;
91        let x24 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
92        //~^ ERROR: clamp-like pattern without using clamp function
93        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
94        let x25 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
95        //~^ ERROR: clamp-like pattern without using clamp function
96        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
97        let x26 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
98        //~^ ERROR: clamp-like pattern without using clamp function
99        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
100        let x27 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
101        //~^ ERROR: clamp-like pattern without using clamp function
102        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
103        let x28 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
104        //~^ ERROR: clamp-like pattern without using clamp function
105        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
106        let x29 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
107        //~^ ERROR: clamp-like pattern without using clamp function
108        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
109        let x30 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
110        //~^ ERROR: clamp-like pattern without using clamp function
111        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
112        let x31 = input.clamp(CONST_F64_MIN, CONST_F64_MAX);
113        //~^ ERROR: clamp-like pattern without using clamp function
114        //~| NOTE: clamp will panic if max < min, min.is_nan(), or max.is_nan()
115    }
116    let mut x32 = input;
117    x32 = x32.clamp(min, max);
118
119    // It's important this be the last set of statements
120    let mut x33 = input;
121    x33 = x33.clamp(min, max);
122}
123
124// This code intentionally nonsense.
125fn no_lint() {
126    let (input, min, max) = (0, -2, 3);
127    let x0 = if max < input {
128        max
129    } else if min > input {
130        max
131    } else {
132        min
133    };
134
135    let x1 = if input > max {
136        max
137    } else if input > min {
138        min
139    } else {
140        max
141    };
142
143    let x2 = if max < min {
144        min
145    } else if input > max {
146        input
147    } else {
148        input
149    };
150
151    let x3 = if min > input {
152        input
153    } else if max < input {
154        max
155    } else {
156        max
157    };
158
159    let x6 = match input {
160        x if x < max => x,
161        x if x < min => x,
162        x => x,
163    };
164
165    let x7 = match input {
166        x if x < min => max,
167        x if x > max => min,
168        x => x,
169    };
170
171    let x8 = match input {
172        x if max > x => max,
173        x if min > x => min,
174        x => x,
175    };
176
177    let mut x9 = input;
178    if x9 > min {
179        x9 = min;
180    }
181    if x9 > max {
182        x9 = max;
183    }
184
185    let x10 = match input {
186        x if min > x => min,
187        x if max < x => max,
188        x => min,
189    };
190
191    let mut x11 = input;
192    if x11 > max {
193        x11 = min;
194    }
195    if x11 < min {
196        x11 = max;
197    }
198
199    let mut x12 = input;
200    if min > x12 {
201        x12 = max * 3;
202    }
203    if max < x12 {
204        x12 = min;
205    }
206
207    let mut x13 = input;
208    if max < x13 {
209        let x13 = max;
210    }
211    if min > x13 {
212        x13 = min;
213    }
214    let mut x14 = input;
215    if x14 < min {
216        x14 = 3;
217    } else if x14 > max {
218        x14 = max;
219    }
220    {
221        let input: i32 = cmp_min_max(1);
222        // These can only be detected if exactly one of the arguments to the inner function is const.
223        let x16 = cmp_max(cmp_max(input, CONST_MAX), CONST_MIN);
224        let x17 = cmp_min(cmp_min(input, CONST_MIN), CONST_MAX);
225        let x18 = cmp_max(CONST_MIN, cmp_max(input, CONST_MAX));
226        let x19 = cmp_min(CONST_MAX, cmp_min(input, CONST_MIN));
227        let x20 = cmp_max(cmp_max(CONST_MAX, input), CONST_MIN);
228        let x21 = cmp_min(cmp_min(CONST_MIN, input), CONST_MAX);
229        let x22 = cmp_max(CONST_MIN, cmp_max(CONST_MAX, input));
230        let x23 = cmp_min(CONST_MAX, cmp_min(CONST_MIN, input));
231        let input: f64 = cmp_min_max(1) as f64;
232        let x24 = f64::max(f64::max(input, CONST_F64_MAX), CONST_F64_MIN);
233        let x25 = f64::min(f64::min(input, CONST_F64_MIN), CONST_F64_MAX);
234        let x26 = f64::max(CONST_F64_MIN, f64::max(input, CONST_F64_MAX));
235        let x27 = f64::min(CONST_F64_MAX, f64::min(input, CONST_F64_MIN));
236        let x28 = f64::max(f64::max(CONST_F64_MAX, input), CONST_F64_MIN);
237        let x29 = f64::min(f64::min(CONST_F64_MIN, input), CONST_F64_MAX);
238        let x30 = f64::max(CONST_F64_MIN, f64::max(CONST_F64_MAX, input));
239        let x31 = f64::min(CONST_F64_MAX, f64::min(CONST_F64_MIN, input));
240        let x32 = f64::min(CONST_F64_MAX, f64::min(CONST_F64_MIN, CONST_F64_MAX));
241    }
242}
243
244fn dont_tell_me_what_to_do() {
245    let (input, min, max) = (0, -2, 3);
246    let mut x_never = input;
247    #[allow(clippy::manual_clamp)]
248    if x_never < min {
249        x_never = min;
250    }
251    if x_never > max {
252        x_never = max;
253    }
254}
255
256/// Just to ensure this isn't const evaled
257fn cmp_min_max(input: i32) -> i32 {
258    input * 3
259}
260
261#[clippy::msrv = "1.49"]
262fn msrv_1_49() {
263    let (input, min, max) = (0, -1, 2);
264    let _ = if input < min {
265        min
266    } else if input > max {
267        max
268    } else {
269        input
270    };
271}
272
273#[clippy::msrv = "1.50"]
274fn msrv_1_50() {
275    let (input, min, max) = (0, -1, 2);
276    let _ = input.clamp(min, max);
277}
278
279const fn _const() {
280    let (input, min, max) = (0, -1, 2);
281    let _ = if input < min {
282        min
283    } else if input > max {
284        max
285    } else {
286        input
287    };
288
289    let mut x = input;
290    if max < x {
291        let x = max;
292    }
293    if min > x {
294        x = min;
295    }
296}