Auto merge of #12070 - roife:fix/issue-12034, r=Centri3

Fix issue #12034: add autofixes for unnecessary_fallible_conversions

fixes #12034

Currently, the `unnecessary_fallible_conversions` lint was capable of autofixing expressions like `0i32.try_into().unwrap()`. However, it couldn't autofix expressions in the form of `i64::try_from(0i32).unwrap()` or `<i64 as TryFrom<i32>>::try_from(0).unwrap()`.

This pull request extends the functionality to correctly autofix these latter forms as well.

changelog: [`unnecessary_fallible_conversions`]: Add autofixes for more forms
This commit is contained in:
bors 2024-02-09 17:37:26 +00:00
commit 28443e63fb
4 changed files with 292 additions and 45 deletions

View file

@ -1,6 +1,43 @@
#![warn(clippy::unnecessary_fallible_conversions)]
fn main() {
// --- TryFromMethod `T::try_from(u)` ---
let _: i64 = 0i32.into();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _: i64 = 0i32.into();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryFromFunction `T::try_from(U)` ---
let _ = i64::from(0i32);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _ = i64::from(0i32);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryIntoFunction `U::try_into(t)` ---
let _: i64 = i32::into(0);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _: i64 = i32::into(0i32);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryFromFunction `<T as TryFrom<U>>::try_from(U)` ---
let _ = <i64 as From<i32>>::from(0);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _ = <i64 as From<i32>>::from(0);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryIntoFunction `<U as TryInto<_>>::try_into(U)` ---
let _: i64 = <i32 as Into<_>>::into(0);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _: i64 = <i32 as Into<_>>::into(0);
//~^ ERROR: use of a fallible conversion when an infallible one could be used
}

View file

@ -1,6 +1,43 @@
#![warn(clippy::unnecessary_fallible_conversions)]
fn main() {
// --- TryFromMethod `T::try_from(u)` ---
let _: i64 = 0i32.try_into().unwrap();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _: i64 = 0i32.try_into().expect("can't happen");
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryFromFunction `T::try_from(U)` ---
let _ = i64::try_from(0i32).unwrap();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _ = i64::try_from(0i32).expect("can't happen");
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryIntoFunction `U::try_into(t)` ---
let _: i64 = i32::try_into(0).unwrap();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _: i64 = i32::try_into(0i32).expect("can't happen");
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryFromFunction `<T as TryFrom<U>>::try_from(U)` ---
let _ = <i64 as TryFrom<i32>>::try_from(0).unwrap();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _ = <i64 as TryFrom<i32>>::try_from(0).expect("can't happen");
//~^ ERROR: use of a fallible conversion when an infallible one could be used
// --- TryIntoFunction `<U as TryInto<_>>::try_into(U)` ---
let _: i64 = <i32 as TryInto<_>>::try_into(0).unwrap();
//~^ ERROR: use of a fallible conversion when an infallible one could be used
let _: i64 = <i32 as TryInto<_>>::try_into(0).expect("can't happen");
//~^ ERROR: use of a fallible conversion when an infallible one could be used
}

View file

@ -1,20 +1,134 @@
error: use of a fallible conversion when an infallible one could be used
--> $DIR/unnecessary_fallible_conversions.rs:4:23
--> $DIR/unnecessary_fallible_conversions.rs:6: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
--> $DIR/unnecessary_fallible_conversions.rs:9: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:14: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:17: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:22: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:25: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:30:13
|
LL | let _ = <i64 as TryFrom<i32>>::try_from(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _ = <i64 as TryFrom<i32>>::try_from(0).unwrap();
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:33:13
|
LL | let _ = <i64 as TryFrom<i32>>::try_from(0).expect("can't happen");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: converting `i32` to `i64` cannot fail
help: use
|
LL - let _ = <i64 as TryFrom<i32>>::try_from(0).expect("can't happen");
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:38: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:41: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