rust-clippy
rust-clippy copied to clipboard
unnecessary_fallible_conversions can't `into` a `Result`, missing trait impl
Using the following flags
--force-warn clippy::unnecessary_fallible_conversions
this code:
//@ check-pass
//@ edition:2021
#![warn(redundant_imports)]
use std::convert::TryFrom;//~ WARNING the item `TryFrom` is imported redundantly
use std::convert::TryInto;//~ WARNING the item `TryInto` is imported redundantly
fn main() {
let _e: Result<i32, _> = 8u8.try_into();
let _f: Result<i32, _> = i32::try_from(8u8);
}
caused the following diagnostics:
Checking _use-redundant-prelude-rust-2021 v0.1.0 (/tmp/icemaker_global_tempdir.o4hDRjvshrmO/icemaker_clippyfix_tempdir.S6ziQ0lIGuC1/_use-redundant-prelude-rust-2021)
warning: use of a fallible conversion when an infallible one could be used
--> src/main.rs:9:34
|
9 | let _e: Result<i32, _> = 8u8.try_into();
| ^^^^^^^^ help: use: `into`
|
= note: converting `u8` to `i32` cannot fail
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fallible_conversions
= note: requested on the command line with `--force-warn clippy::unnecessary-fallible-conversions`
warning: use of a fallible conversion when an infallible one could be used
--> src/main.rs:10:30
|
10 | let _f: Result<i32, _> = i32::try_from(8u8);
| ^^^^^^^^^^^^^ help: use: `From::from`
|
= note: converting `u8` to `i32` cannot fail
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fallible_conversions
warning: `_use-redundant-prelude-rust-2021` (bin "_use-redundant-prelude-rust-2021") generated 2 warnings
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
However after applying these diagnostics, the resulting code:
//@ check-pass
//@ edition:2021
#![warn(redundant_imports)]
use std::convert::TryFrom;//~ WARNING the item `TryFrom` is imported redundantly
use std::convert::TryInto;//~ WARNING the item `TryInto` is imported redundantly
fn main() {
let _e: Result<i32, _> = 8u8.into();
let _f: Result<i32, _> = From::from(8u8);
}
no longer compiled:
Checking _use-redundant-prelude-rust-2021 v0.1.0 (/tmp/icemaker_global_tempdir.o4hDRjvshrmO/icemaker_clippyfix_tempdir.S6ziQ0lIGuC1/_use-redundant-prelude-rust-2021)
error[E0277]: the trait bound `std::result::Result<i32, _>: std::convert::From<u8>` is not satisfied
--> src/main.rs:9:34
|
9 | let _e: Result<i32, _> = 8u8.into();
| ^^^^ the trait `std::convert::From<u8>` is not implemented for `std::result::Result<i32, _>`
|
= note: required for `u8` to implement `std::convert::Into<std::result::Result<i32, _>>`
error[E0277]: the trait bound `std::result::Result<i32, _>: std::convert::From<u8>` is not satisfied
--> src/main.rs:10:41
|
10 | let _f: Result<i32, _> = From::from(8u8);
| ---------- ^^^ the trait `std::convert::From<u8>` is not implemented for `std::result::Result<i32, _>`
| |
| required by a bound introduced by this call
For more information about this error, try `rustc --explain E0277`.
error: could not compile `_use-redundant-prelude-rust-2021` (bin "_use-redundant-prelude-rust-2021" test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_use-redundant-prelude-rust-2021` (bin "_use-redundant-prelude-rust-2021") due to 2 previous errors
Version:
rustc 1.89.0-nightly (cf423712b 2025-06-05)
binary: rustc
commit-hash: cf423712b9e95e9f6ec84b1ecb3d125e55ac8d56
commit-date: 2025-06-05
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
@rustbot claim
From the lint's description:
let _: Result<i64, _> = 1i32.try_into();
let _: Result<i64, _> = <_>::try_from(1i32);
// Use `from`/`into` instead:
let _: i64 = 1i32.into();
let _: i64 = <_>::from(1i32);
And since the applicability here is Unspecified, I think it is intended here to also change the type annotation, not just apply the help directly.