Implement manual_clamp lint
This commit is contained in:
parent
31b17411a6
commit
b221184572
16 changed files with 1493 additions and 33 deletions
304
tests/ui/manual_clamp.rs
Normal file
304
tests/ui/manual_clamp.rs
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
#![warn(clippy::manual_clamp)]
|
||||
#![allow(
|
||||
unused,
|
||||
dead_code,
|
||||
clippy::unnecessary_operation,
|
||||
clippy::no_effect,
|
||||
clippy::if_same_then_else
|
||||
)]
|
||||
|
||||
use std::cmp::{max as cmp_max, min as cmp_min};
|
||||
|
||||
const CONST_MAX: i32 = 10;
|
||||
const CONST_MIN: i32 = 4;
|
||||
|
||||
const CONST_F64_MAX: f64 = 10.0;
|
||||
const CONST_F64_MIN: f64 = 4.0;
|
||||
|
||||
fn main() {
|
||||
let (input, min, max) = (0, -2, 3);
|
||||
// Lint
|
||||
let x0 = if max < input {
|
||||
max
|
||||
} else if min > input {
|
||||
min
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
let x1 = if input > max {
|
||||
max
|
||||
} else if input < min {
|
||||
min
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
let x2 = if input < min {
|
||||
min
|
||||
} else if input > max {
|
||||
max
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
let x3 = if min > input {
|
||||
min
|
||||
} else if max < input {
|
||||
max
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
let x4 = input.max(min).min(max);
|
||||
|
||||
let x5 = input.min(max).max(min);
|
||||
|
||||
let x6 = match input {
|
||||
x if x > max => max,
|
||||
x if x < min => min,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let x7 = match input {
|
||||
x if x < min => min,
|
||||
x if x > max => max,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let x8 = match input {
|
||||
x if max < x => max,
|
||||
x if min > x => min,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let mut x9 = input;
|
||||
if x9 < min {
|
||||
x9 = min;
|
||||
}
|
||||
if x9 > max {
|
||||
x9 = max;
|
||||
}
|
||||
|
||||
let x10 = match input {
|
||||
x if min > x => min,
|
||||
x if max < x => max,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let mut x11 = input;
|
||||
let _ = 1;
|
||||
if x11 > max {
|
||||
x11 = max;
|
||||
}
|
||||
if x11 < min {
|
||||
x11 = min;
|
||||
}
|
||||
|
||||
let mut x12 = input;
|
||||
if min > x12 {
|
||||
x12 = min;
|
||||
}
|
||||
if max < x12 {
|
||||
x12 = max;
|
||||
}
|
||||
|
||||
let mut x13 = input;
|
||||
if max < x13 {
|
||||
x13 = max;
|
||||
}
|
||||
if min > x13 {
|
||||
x13 = min;
|
||||
}
|
||||
|
||||
let x14 = if input > CONST_MAX {
|
||||
CONST_MAX
|
||||
} else if input < CONST_MIN {
|
||||
CONST_MIN
|
||||
} else {
|
||||
input
|
||||
};
|
||||
{
|
||||
let (input, min, max) = (0.0f64, -2.0, 3.0);
|
||||
let x15 = if input > max {
|
||||
max
|
||||
} else if input < min {
|
||||
min
|
||||
} else {
|
||||
input
|
||||
};
|
||||
}
|
||||
{
|
||||
let input: i32 = cmp_min_max(1);
|
||||
// These can only be detected if exactly one of the arguments to the inner function is const.
|
||||
let x16 = cmp_max(cmp_min(input, CONST_MAX), CONST_MIN);
|
||||
let x17 = cmp_min(cmp_max(input, CONST_MIN), CONST_MAX);
|
||||
let x18 = cmp_max(CONST_MIN, cmp_min(input, CONST_MAX));
|
||||
let x19 = cmp_min(CONST_MAX, cmp_max(input, CONST_MIN));
|
||||
let x20 = cmp_max(cmp_min(CONST_MAX, input), CONST_MIN);
|
||||
let x21 = cmp_min(cmp_max(CONST_MIN, input), CONST_MAX);
|
||||
let x22 = cmp_max(CONST_MIN, cmp_min(CONST_MAX, input));
|
||||
let x23 = cmp_min(CONST_MAX, cmp_max(CONST_MIN, input));
|
||||
let input: f64 = cmp_min_max(1) as f64;
|
||||
let x24 = f64::max(f64::min(input, CONST_F64_MAX), CONST_F64_MIN);
|
||||
let x25 = f64::min(f64::max(input, CONST_F64_MIN), CONST_F64_MAX);
|
||||
let x26 = f64::max(CONST_F64_MIN, f64::min(input, CONST_F64_MAX));
|
||||
let x27 = f64::min(CONST_F64_MAX, f64::max(input, CONST_F64_MIN));
|
||||
let x28 = f64::max(f64::min(CONST_F64_MAX, input), CONST_F64_MIN);
|
||||
let x29 = f64::min(f64::max(CONST_F64_MIN, input), CONST_F64_MAX);
|
||||
let x30 = f64::max(CONST_F64_MIN, f64::min(CONST_F64_MAX, input));
|
||||
let x31 = f64::min(CONST_F64_MAX, f64::max(CONST_F64_MIN, input));
|
||||
}
|
||||
let mut x32 = input;
|
||||
if x32 < min {
|
||||
x32 = min;
|
||||
} else if x32 > max {
|
||||
x32 = max;
|
||||
}
|
||||
|
||||
// It's important this be the last set of statements
|
||||
let mut x33 = input;
|
||||
if max < x33 {
|
||||
x33 = max;
|
||||
}
|
||||
if min > x33 {
|
||||
x33 = min;
|
||||
}
|
||||
}
|
||||
|
||||
// This code intentionally nonsense.
|
||||
fn no_lint() {
|
||||
let (input, min, max) = (0, -2, 3);
|
||||
let x0 = if max < input {
|
||||
max
|
||||
} else if min > input {
|
||||
max
|
||||
} else {
|
||||
min
|
||||
};
|
||||
|
||||
let x1 = if input > max {
|
||||
max
|
||||
} else if input > min {
|
||||
min
|
||||
} else {
|
||||
max
|
||||
};
|
||||
|
||||
let x2 = if max < min {
|
||||
min
|
||||
} else if input > max {
|
||||
input
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
let x3 = if min > input {
|
||||
input
|
||||
} else if max < input {
|
||||
max
|
||||
} else {
|
||||
max
|
||||
};
|
||||
|
||||
let x6 = match input {
|
||||
x if x < max => x,
|
||||
x if x < min => x,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let x7 = match input {
|
||||
x if x < min => max,
|
||||
x if x > max => min,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let x8 = match input {
|
||||
x if max > x => max,
|
||||
x if min > x => min,
|
||||
x => x,
|
||||
};
|
||||
|
||||
let mut x9 = input;
|
||||
if x9 > min {
|
||||
x9 = min;
|
||||
}
|
||||
if x9 > max {
|
||||
x9 = max;
|
||||
}
|
||||
|
||||
let x10 = match input {
|
||||
x if min > x => min,
|
||||
x if max < x => max,
|
||||
x => min,
|
||||
};
|
||||
|
||||
let mut x11 = input;
|
||||
if x11 > max {
|
||||
x11 = min;
|
||||
}
|
||||
if x11 < min {
|
||||
x11 = max;
|
||||
}
|
||||
|
||||
let mut x12 = input;
|
||||
if min > x12 {
|
||||
x12 = max * 3;
|
||||
}
|
||||
if max < x12 {
|
||||
x12 = min;
|
||||
}
|
||||
|
||||
let mut x13 = input;
|
||||
if max < x13 {
|
||||
let x13 = max;
|
||||
}
|
||||
if min > x13 {
|
||||
x13 = min;
|
||||
}
|
||||
let mut x14 = input;
|
||||
if x14 < min {
|
||||
x14 = 3;
|
||||
} else if x14 > max {
|
||||
x14 = max;
|
||||
}
|
||||
{
|
||||
let input: i32 = cmp_min_max(1);
|
||||
// These can only be detected if exactly one of the arguments to the inner function is const.
|
||||
let x16 = cmp_max(cmp_max(input, CONST_MAX), CONST_MIN);
|
||||
let x17 = cmp_min(cmp_min(input, CONST_MIN), CONST_MAX);
|
||||
let x18 = cmp_max(CONST_MIN, cmp_max(input, CONST_MAX));
|
||||
let x19 = cmp_min(CONST_MAX, cmp_min(input, CONST_MIN));
|
||||
let x20 = cmp_max(cmp_max(CONST_MAX, input), CONST_MIN);
|
||||
let x21 = cmp_min(cmp_min(CONST_MIN, input), CONST_MAX);
|
||||
let x22 = cmp_max(CONST_MIN, cmp_max(CONST_MAX, input));
|
||||
let x23 = cmp_min(CONST_MAX, cmp_min(CONST_MIN, input));
|
||||
let input: f64 = cmp_min_max(1) as f64;
|
||||
let x24 = f64::max(f64::max(input, CONST_F64_MAX), CONST_F64_MIN);
|
||||
let x25 = f64::min(f64::min(input, CONST_F64_MIN), CONST_F64_MAX);
|
||||
let x26 = f64::max(CONST_F64_MIN, f64::max(input, CONST_F64_MAX));
|
||||
let x27 = f64::min(CONST_F64_MAX, f64::min(input, CONST_F64_MIN));
|
||||
let x28 = f64::max(f64::max(CONST_F64_MAX, input), CONST_F64_MIN);
|
||||
let x29 = f64::min(f64::min(CONST_F64_MIN, input), CONST_F64_MAX);
|
||||
let x30 = f64::max(CONST_F64_MIN, f64::max(CONST_F64_MAX, input));
|
||||
let x31 = f64::min(CONST_F64_MAX, f64::min(CONST_F64_MIN, input));
|
||||
let x32 = f64::min(CONST_F64_MAX, f64::min(CONST_F64_MIN, CONST_F64_MAX));
|
||||
}
|
||||
}
|
||||
|
||||
fn dont_tell_me_what_to_do() {
|
||||
let (input, min, max) = (0, -2, 3);
|
||||
let mut x_never = input;
|
||||
#[allow(clippy::manual_clamp)]
|
||||
if x_never < min {
|
||||
x_never = min;
|
||||
}
|
||||
if x_never > max {
|
||||
x_never = max;
|
||||
}
|
||||
}
|
||||
|
||||
/// Just to ensure this isn't const evaled
|
||||
fn cmp_min_max(input: i32) -> i32 {
|
||||
input * 3
|
||||
}
|
||||
375
tests/ui/manual_clamp.stderr
Normal file
375
tests/ui/manual_clamp.stderr
Normal file
|
|
@ -0,0 +1,375 @@
|
|||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:76:5
|
||||
|
|
||||
LL | / if x9 < min {
|
||||
LL | | x9 = min;
|
||||
LL | | }
|
||||
LL | | if x9 > max {
|
||||
LL | | x9 = max;
|
||||
LL | | }
|
||||
| |_____^ help: replace with clamp: `x9 = x9.clamp(min, max);`
|
||||
|
|
||||
= note: `-D clippy::manual-clamp` implied by `-D warnings`
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:91:5
|
||||
|
|
||||
LL | / if x11 > max {
|
||||
LL | | x11 = max;
|
||||
LL | | }
|
||||
LL | | if x11 < min {
|
||||
LL | | x11 = min;
|
||||
LL | | }
|
||||
| |_____^ help: replace with clamp: `x11 = x11.clamp(min, max);`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:99:5
|
||||
|
|
||||
LL | / if min > x12 {
|
||||
LL | | x12 = min;
|
||||
LL | | }
|
||||
LL | | if max < x12 {
|
||||
LL | | x12 = max;
|
||||
LL | | }
|
||||
| |_____^ help: replace with clamp: `x12 = x12.clamp(min, max);`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:107:5
|
||||
|
|
||||
LL | / if max < x13 {
|
||||
LL | | x13 = max;
|
||||
LL | | }
|
||||
LL | | if min > x13 {
|
||||
LL | | x13 = min;
|
||||
LL | | }
|
||||
| |_____^ help: replace with clamp: `x13 = x13.clamp(min, max);`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:161:5
|
||||
|
|
||||
LL | / if max < x33 {
|
||||
LL | | x33 = max;
|
||||
LL | | }
|
||||
LL | | if min > x33 {
|
||||
LL | | x33 = min;
|
||||
LL | | }
|
||||
| |_____^ help: replace with clamp: `x33 = x33.clamp(min, max);`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:21:14
|
||||
|
|
||||
LL | let x0 = if max < input {
|
||||
| ______________^
|
||||
LL | | max
|
||||
LL | | } else if min > input {
|
||||
LL | | min
|
||||
LL | | } else {
|
||||
LL | | input
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:29:14
|
||||
|
|
||||
LL | let x1 = if input > max {
|
||||
| ______________^
|
||||
LL | | max
|
||||
LL | | } else if input < min {
|
||||
LL | | min
|
||||
LL | | } else {
|
||||
LL | | input
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:37:14
|
||||
|
|
||||
LL | let x2 = if input < min {
|
||||
| ______________^
|
||||
LL | | min
|
||||
LL | | } else if input > max {
|
||||
LL | | max
|
||||
LL | | } else {
|
||||
LL | | input
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:45:14
|
||||
|
|
||||
LL | let x3 = if min > input {
|
||||
| ______________^
|
||||
LL | | min
|
||||
LL | | } else if max < input {
|
||||
LL | | max
|
||||
LL | | } else {
|
||||
LL | | input
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:53:14
|
||||
|
|
||||
LL | let x4 = input.max(min).min(max);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:55:14
|
||||
|
|
||||
LL | let x5 = input.min(max).max(min);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:57:14
|
||||
|
|
||||
LL | let x6 = match input {
|
||||
| ______________^
|
||||
LL | | x if x > max => max,
|
||||
LL | | x if x < min => min,
|
||||
LL | | x => x,
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:63:14
|
||||
|
|
||||
LL | let x7 = match input {
|
||||
| ______________^
|
||||
LL | | x if x < min => min,
|
||||
LL | | x if x > max => max,
|
||||
LL | | x => x,
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:69:14
|
||||
|
|
||||
LL | let x8 = match input {
|
||||
| ______________^
|
||||
LL | | x if max < x => max,
|
||||
LL | | x if min > x => min,
|
||||
LL | | x => x,
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:83:15
|
||||
|
|
||||
LL | let x10 = match input {
|
||||
| _______________^
|
||||
LL | | x if min > x => min,
|
||||
LL | | x if max < x => max,
|
||||
LL | | x => x,
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:114:15
|
||||
|
|
||||
LL | let x14 = if input > CONST_MAX {
|
||||
| _______________^
|
||||
LL | | CONST_MAX
|
||||
LL | | } else if input < CONST_MIN {
|
||||
LL | | CONST_MIN
|
||||
LL | | } else {
|
||||
LL | | input
|
||||
LL | | };
|
||||
| |_____^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:123:19
|
||||
|
|
||||
LL | let x15 = if input > max {
|
||||
| ___________________^
|
||||
LL | | max
|
||||
LL | | } else if input < min {
|
||||
LL | | min
|
||||
LL | | } else {
|
||||
LL | | input
|
||||
LL | | };
|
||||
| |_________^ help: replace with clamp: `input.clamp(min, max)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:134:19
|
||||
|
|
||||
LL | let x16 = cmp_max(cmp_min(input, CONST_MAX), CONST_MIN);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:135:19
|
||||
|
|
||||
LL | let x17 = cmp_min(cmp_max(input, CONST_MIN), CONST_MAX);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:136:19
|
||||
|
|
||||
LL | let x18 = cmp_max(CONST_MIN, cmp_min(input, CONST_MAX));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:137:19
|
||||
|
|
||||
LL | let x19 = cmp_min(CONST_MAX, cmp_max(input, CONST_MIN));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:138:19
|
||||
|
|
||||
LL | let x20 = cmp_max(cmp_min(CONST_MAX, input), CONST_MIN);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:139:19
|
||||
|
|
||||
LL | let x21 = cmp_min(cmp_max(CONST_MIN, input), CONST_MAX);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:140:19
|
||||
|
|
||||
LL | let x22 = cmp_max(CONST_MIN, cmp_min(CONST_MAX, input));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:141:19
|
||||
|
|
||||
LL | let x23 = cmp_min(CONST_MAX, cmp_max(CONST_MIN, input));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:143:19
|
||||
|
|
||||
LL | let x24 = f64::max(f64::min(input, CONST_F64_MAX), CONST_F64_MIN);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:144:19
|
||||
|
|
||||
LL | let x25 = f64::min(f64::max(input, CONST_F64_MIN), CONST_F64_MAX);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:145:19
|
||||
|
|
||||
LL | let x26 = f64::max(CONST_F64_MIN, f64::min(input, CONST_F64_MAX));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:146:19
|
||||
|
|
||||
LL | let x27 = f64::min(CONST_F64_MAX, f64::max(input, CONST_F64_MIN));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:147:19
|
||||
|
|
||||
LL | let x28 = f64::max(f64::min(CONST_F64_MAX, input), CONST_F64_MIN);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:148:19
|
||||
|
|
||||
LL | let x29 = f64::min(f64::max(CONST_F64_MIN, input), CONST_F64_MAX);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:149:19
|
||||
|
|
||||
LL | let x30 = f64::max(CONST_F64_MIN, f64::min(CONST_F64_MAX, input));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:150:19
|
||||
|
|
||||
LL | let x31 = f64::min(CONST_F64_MAX, f64::max(CONST_F64_MIN, input));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)`
|
||||
|
|
||||
= note: clamp will panic if max < min, min.is_nan(), or max.is_nan()
|
||||
= note: clamp returns NaN if the input is NaN
|
||||
|
||||
error: clamp-like pattern without using clamp function
|
||||
--> $DIR/manual_clamp.rs:153:5
|
||||
|
|
||||
LL | / if x32 < min {
|
||||
LL | | x32 = min;
|
||||
LL | | } else if x32 > max {
|
||||
LL | | x32 = max;
|
||||
LL | | }
|
||||
| |_____^ help: replace with clamp: `x32 = x32.clamp(min, max);`
|
||||
|
|
||||
= note: clamp will panic if max < min
|
||||
|
||||
error: aborting due to 34 previous errors
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#![warn(clippy::all)]
|
||||
#![allow(clippy::manual_clamp)]
|
||||
|
||||
use std::cmp::max as my_max;
|
||||
use std::cmp::min as my_min;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:23:5
|
||||
--> $DIR/min_max.rs:24:5
|
||||
|
|
||||
LL | min(1, max(3, x));
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
@ -7,73 +7,73 @@ LL | min(1, max(3, x));
|
|||
= note: `-D clippy::min-max` implied by `-D warnings`
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:24:5
|
||||
--> $DIR/min_max.rs:25:5
|
||||
|
|
||||
LL | min(max(3, x), 1);
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:25:5
|
||||
--> $DIR/min_max.rs:26:5
|
||||
|
|
||||
LL | max(min(x, 1), 3);
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:26:5
|
||||
--> $DIR/min_max.rs:27:5
|
||||
|
|
||||
LL | max(3, min(x, 1));
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:28:5
|
||||
--> $DIR/min_max.rs:29:5
|
||||
|
|
||||
LL | my_max(3, my_min(x, 1));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:38:5
|
||||
--> $DIR/min_max.rs:39:5
|
||||
|
|
||||
LL | min("Apple", max("Zoo", s));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:39:5
|
||||
--> $DIR/min_max.rs:40:5
|
||||
|
|
||||
LL | max(min(s, "Apple"), "Zoo");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:44:5
|
||||
--> $DIR/min_max.rs:45:5
|
||||
|
|
||||
LL | x.min(1).max(3);
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:45:5
|
||||
--> $DIR/min_max.rs:46:5
|
||||
|
|
||||
LL | x.max(3).min(1);
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:46:5
|
||||
--> $DIR/min_max.rs:47:5
|
||||
|
|
||||
LL | f.max(3f32).min(1f32);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:52:5
|
||||
--> $DIR/min_max.rs:53:5
|
||||
|
|
||||
LL | max(x.min(1), 3);
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:55:5
|
||||
--> $DIR/min_max.rs:56:5
|
||||
|
|
||||
LL | s.max("Zoo").min("Apple");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this `min`/`max` combination leads to constant result
|
||||
--> $DIR/min_max.rs:56:5
|
||||
--> $DIR/min_max.rs:57:5
|
||||
|
|
||||
LL | s.min("Apple").max("Zoo");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
|
|||
|
|
@ -160,6 +160,17 @@ fn manual_rem_euclid() {
|
|||
let _: i32 = ((x % 4) + 4) % 4;
|
||||
}
|
||||
|
||||
fn manual_clamp() {
|
||||
let (input, min, max) = (0, -1, 2);
|
||||
let _ = if input < min {
|
||||
min
|
||||
} else if input > max {
|
||||
max
|
||||
} else {
|
||||
input
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
filter_map_next();
|
||||
checked_conversion();
|
||||
|
|
@ -180,6 +191,7 @@ fn main() {
|
|||
err_expect();
|
||||
cast_abs_to_unsigned();
|
||||
manual_rem_euclid();
|
||||
manual_clamp();
|
||||
}
|
||||
|
||||
mod just_under_msrv {
|
||||
|
|
|
|||
|
|
@ -1,27 +1,10 @@
|
|||
error: stripping a prefix manually
|
||||
--> $DIR/min_rust_version_attr.rs:204:24
|
||||
|
|
||||
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::manual-strip` implied by `-D warnings`
|
||||
note: the prefix was tested here
|
||||
--> $DIR/min_rust_version_attr.rs:203:9
|
||||
|
|
||||
LL | if s.starts_with("hello, ") {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: try using the `strip_prefix` method
|
||||
|
|
||||
LL ~ if let Some(<stripped>) = s.strip_prefix("hello, ") {
|
||||
LL ~ assert_eq!(<stripped>.to_uppercase(), "WORLD!");
|
||||
|
|
||||
|
||||
error: stripping a prefix manually
|
||||
--> $DIR/min_rust_version_attr.rs:216:24
|
||||
|
|
||||
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::manual-strip` implied by `-D warnings`
|
||||
note: the prefix was tested here
|
||||
--> $DIR/min_rust_version_attr.rs:215:9
|
||||
|
|
||||
|
|
@ -33,5 +16,22 @@ LL ~ if let Some(<stripped>) = s.strip_prefix("hello, ") {
|
|||
LL ~ assert_eq!(<stripped>.to_uppercase(), "WORLD!");
|
||||
|
|
||||
|
||||
error: stripping a prefix manually
|
||||
--> $DIR/min_rust_version_attr.rs:228:24
|
||||
|
|
||||
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: the prefix was tested here
|
||||
--> $DIR/min_rust_version_attr.rs:227:9
|
||||
|
|
||||
LL | if s.starts_with("hello, ") {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: try using the `strip_prefix` method
|
||||
|
|
||||
LL ~ if let Some(<stripped>) = s.strip_prefix("hello, ") {
|
||||
LL ~ assert_eq!(<stripped>.to_uppercase(), "WORLD!");
|
||||
|
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue