diff options
| author | Paolo Borelli <pborelli@gnome.org> | 2022-04-04 18:38:38 +0200 |
|---|---|---|
| committer | Paolo Borelli <pborelli@gnome.org> | 2022-04-07 11:28:14 +0200 |
| commit | b0edbca0e6e4ce771bc5ecf54b9b35a824af07aa (patch) | |
| tree | fef49142e5caea54289a48efcf7959847438a67d /tests | |
| parent | 0d664049417976a5889e6a3354fdac00249e3a9a (diff) | |
| download | rust-b0edbca0e6e4ce771bc5ecf54b9b35a824af07aa.tar.gz rust-b0edbca0e6e4ce771bc5ecf54b9b35a824af07aa.zip | |
new lint cast_abs_to_unsigned
Add a lint to detect cast to unsigned for abs() and suggest unsigned_abs() to avoid panic when called on MIN.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/cast.rs | 2 | ||||
| -rw-r--r-- | tests/ui/cast_abs_to_unsigned.fixed | 8 | ||||
| -rw-r--r-- | tests/ui/cast_abs_to_unsigned.rs | 8 | ||||
| -rw-r--r-- | tests/ui/cast_abs_to_unsigned.stderr | 10 | ||||
| -rw-r--r-- | tests/ui/min_rust_version_attr.rs | 6 | ||||
| -rw-r--r-- | tests/ui/min_rust_version_attr.stderr | 8 |
6 files changed, 37 insertions, 5 deletions
diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index 2e31ad3172e..cf85a5ca931 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -7,7 +7,7 @@ clippy::cast_sign_loss, clippy::cast_possible_wrap )] -#[allow(clippy::no_effect, clippy::unnecessary_operation)] +#[allow(clippy::cast_abs_to_unsigned, clippy::no_effect, clippy::unnecessary_operation)] fn main() { // Test clippy::cast_precision_loss let x0 = 1i32; diff --git a/tests/ui/cast_abs_to_unsigned.fixed b/tests/ui/cast_abs_to_unsigned.fixed new file mode 100644 index 00000000000..4ec2465be06 --- /dev/null +++ b/tests/ui/cast_abs_to_unsigned.fixed @@ -0,0 +1,8 @@ +// run-rustfix +#![warn(clippy::cast_abs_to_unsigned)] + +fn main() { + let x: i32 = -42; + let y: u32 = x.unsigned_abs(); + println!("The absolute value of {} is {}", x, y); +} diff --git a/tests/ui/cast_abs_to_unsigned.rs b/tests/ui/cast_abs_to_unsigned.rs new file mode 100644 index 00000000000..59b9c8c3678 --- /dev/null +++ b/tests/ui/cast_abs_to_unsigned.rs @@ -0,0 +1,8 @@ +// run-rustfix +#![warn(clippy::cast_abs_to_unsigned)] + +fn main() { + let x: i32 = -42; + let y: u32 = x.abs() as u32; + println!("The absolute value of {} is {}", x, y); +} diff --git a/tests/ui/cast_abs_to_unsigned.stderr b/tests/ui/cast_abs_to_unsigned.stderr new file mode 100644 index 00000000000..eb12857357a --- /dev/null +++ b/tests/ui/cast_abs_to_unsigned.stderr @@ -0,0 +1,10 @@ +error: casting the result of `i32::abs()` to u32 + --> $DIR/cast_abs_to_unsigned.rs:6:18 + | +LL | let y: u32 = x.abs() as u32; + | ^^^^^^^^^^^^^^ help: replace with: `x.unsigned_abs()` + | + = note: `-D clippy::cast-abs-to-unsigned` implied by `-D warnings` + +error: aborting due to previous error + diff --git a/tests/ui/min_rust_version_attr.rs b/tests/ui/min_rust_version_attr.rs index 7666d01ffe1..f83c3e0e281 100644 --- a/tests/ui/min_rust_version_attr.rs +++ b/tests/ui/min_rust_version_attr.rs @@ -150,6 +150,11 @@ fn err_expect() { x.err().expect("Testing expect_err"); } +fn cast_abs_to_unsigned() { + let x: i32 = 10; + assert_eq!(10u32, x.abs() as u32); +} + fn main() { filter_map_next(); checked_conversion(); @@ -168,6 +173,7 @@ fn main() { unnest_or_patterns(); int_from_bool(); err_expect(); + cast_abs_to_unsigned(); } mod just_under_msrv { diff --git a/tests/ui/min_rust_version_attr.stderr b/tests/ui/min_rust_version_attr.stderr index 9ed6308f115..de225eb740d 100644 --- a/tests/ui/min_rust_version_attr.stderr +++ b/tests/ui/min_rust_version_attr.stderr @@ -1,12 +1,12 @@ error: stripping a prefix manually - --> $DIR/min_rust_version_attr.rs:192:24 + --> $DIR/min_rust_version_attr.rs:198: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:191:9 + --> $DIR/min_rust_version_attr.rs:197:9 | LL | if s.starts_with("hello, ") { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -17,13 +17,13 @@ LL ~ assert_eq!(<stripped>.to_uppercase(), "WORLD!"); | error: stripping a prefix manually - --> $DIR/min_rust_version_attr.rs:204:24 + --> $DIR/min_rust_version_attr.rs:210:24 | LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!"); | ^^^^^^^^^^^^^^^^^^^^ | note: the prefix was tested here - --> $DIR/min_rust_version_attr.rs:203:9 + --> $DIR/min_rust_version_attr.rs:209:9 | LL | if s.starts_with("hello, ") { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
