Add tests for autofix in unnecessary_fallible_conversions

This commit is contained in:
roife 2024-01-01 16:17:46 +08:00
parent 84e797499b
commit 69cc983155
3 changed files with 147 additions and 3 deletions

View file

@ -3,4 +3,16 @@
fn main() {
let _: i64 = 0i32.into();
let _: i64 = 0i32.into();
let _ = i64::from(0i32);
let _ = i64::from(0i32);
let _: i64 = i32::into(0);
let _: i64 = i32::into(0i32);
let _ = <i64 as From<i32>>::from(0);
let _ = <i64 as From<i32>>::from(0);
let _: i64 = <i32 as Into<_>>::into(0);
let _: i64 = <i32 as Into<_>>::into(0);
}

View file

@ -3,4 +3,18 @@
fn main() {
let _: i64 = 0i32.try_into().unwrap();
let _: i64 = 0i32.try_into().expect("can't happen");
let _ = i64::try_from(0i32).unwrap();
let _ = i64::try_from(0i32).expect("can't happen");
let _: i64 = i32::try_into(0).unwrap();
let _: i64 = i32::try_into(0i32).expect("can't happen");
let _ = <i64 as TryFrom<i32>>::try_from(0)
.unwrap();
let _ = <i64 as TryFrom<i32>>::try_from(0).
expect("can't happen");
let _: i64 = <i32 as TryInto<_>>::try_into(0).unwrap();
let _: i64 = <i32 as TryInto<_>>::try_into(0).expect("can't happen");
}

View file

@ -2,19 +2,137 @@ error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:4:23
|
LL | let _: i64 = 0i32.try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^ help: use: `into()`
| ^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]`
help: use
|
LL - let _: i64 = 0i32.try_into().unwrap();
LL + let _: i64 = 0i32.into();
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:5:23
|
LL | let _: i64 = 0i32.try_into().expect("can't happen");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `into()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _: i64 = 0i32.try_into().expect("can't happen");
LL + let _: i64 = 0i32.into();
|
error: aborting due to 2 previous errors
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:7:13
|
LL | let _ = i64::try_from(0i32).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _ = i64::try_from(0i32).unwrap();
LL + let _ = i64::from(0i32);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:8:13
|
LL | let _ = i64::try_from(0i32).expect("can't happen");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _ = i64::try_from(0i32).expect("can't happen");
LL + let _ = i64::from(0i32);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:10:18
|
LL | let _: i64 = i32::try_into(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _: i64 = i32::try_into(0).unwrap();
LL + let _: i64 = i32::into(0);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:11:18
|
LL | let _: i64 = i32::try_into(0i32).expect("can't happen");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _: i64 = i32::try_into(0i32).expect("can't happen");
LL + let _: i64 = i32::into(0i32);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:13:13
|
LL | let _ = <i64 as TryFrom<i32>>::try_from(0)
| _____________^
LL | | .unwrap();
| |_________________^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _ = <i64 as TryFrom<i32>>::try_from(0)
LL + let _ = <i64 as From<i32>>::from(0);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:15:13
|
LL | let _ = <i64 as TryFrom<i32>>::try_from(0).
| _____________^
LL | | expect("can't happen");
| |______________________________^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _ = <i64 as TryFrom<i32>>::try_from(0).
LL + let _ = <i64 as From<i32>>::from(0);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:18:18
|
LL | let _: i64 = <i32 as TryInto<_>>::try_into(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _: i64 = <i32 as TryInto<_>>::try_into(0).unwrap();
LL + let _: i64 = <i32 as Into<_>>::into(0);
|
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:19:18
|
LL | let _: i64 = <i32 as TryInto<_>>::try_into(0).expect("can't happen");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _: i64 = <i32 as TryInto<_>>::try_into(0).expect("can't happen");
LL + let _: i64 = <i32 as Into<_>>::into(0);
|
error: aborting due to 10 previous errors