about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-09 17:37:26 +0000
committerbors <bors@rust-lang.org>2024-02-09 17:37:26 +0000
commit28443e63fb633a5ed00a49e8df591a956d77122c (patch)
treecd19d12d897f2e590ddf83739a12a35b2fc7e351 /tests
parentfb398a5777aa0dea7ed53b74770e2c1fb577f4f2 (diff)
parent3c76b2ceff3526b2d46abb1bbd38180131578808 (diff)
downloadrust-28443e63fb633a5ed00a49e8df591a956d77122c.tar.gz
rust-28443e63fb633a5ed00a49e8df591a956d77122c.zip
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
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/unnecessary_fallible_conversions.fixed37
-rw-r--r--tests/ui/unnecessary_fallible_conversions.rs37
-rw-r--r--tests/ui/unnecessary_fallible_conversions.stderr124
3 files changed, 193 insertions, 5 deletions
diff --git a/tests/ui/unnecessary_fallible_conversions.fixed b/tests/ui/unnecessary_fallible_conversions.fixed
index 9668a6b99bf..b6dd1f26774 100644
--- a/tests/ui/unnecessary_fallible_conversions.fixed
+++ b/tests/ui/unnecessary_fallible_conversions.fixed
@@ -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
 }
diff --git a/tests/ui/unnecessary_fallible_conversions.rs b/tests/ui/unnecessary_fallible_conversions.rs
index 9fa6c08b1b0..6f8df7365e8 100644
--- a/tests/ui/unnecessary_fallible_conversions.rs
+++ b/tests/ui/unnecessary_fallible_conversions.rs
@@ -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
 }
diff --git a/tests/ui/unnecessary_fallible_conversions.stderr b/tests/ui/unnecessary_fallible_conversions.stderr
index 26b152515ac..598f4ba91b3 100644
--- a/tests/ui/unnecessary_fallible_conversions.stderr
+++ b/tests/ui/unnecessary_fallible_conversions.stderr
@@ -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: 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 2 previous errors
+error: aborting due to 10 previous errors