Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::unnecessary_fallible_conversions: use of a fallible conversion when an infallible one could be used
What it meansChecks for calls to TryInto::try_into and TryFrom::try_from when their infallible counterparts could be used. It is in the style group and warns by default.
What you see
warning: use of a fallible conversion when an infallible one could be used --> main.rs:3:30 | 3 | let _: Result<i64, _> = 1i32.try_into(); | ^^^^^^^^ | = note: converting `i32` to `i64` cannot fail = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#unnecessary_fallible_conversions = note: `#[warn(clippy::unnecessary_fallible_conversions)]` on by default help: use | 3 - let _: Result<i64, _> = 1i32.try_into(); 3 + let _: Result<i64, _> = 1i32.into(); |
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
In those cases, the TryInto and TryFrom trait implementation is a blanket impl that forwards to Into or From, which always succeeds. The returned Result<_, Infallible> requires error handling to get the contained value even though the conversion can never fail.
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let _: Result<i64, _> = 1i32.try_into();
let _: Result<i64, _> = <_>::try_from(1i32);
Write this instead
let _: i64 = 1i32.into();
let _: i64 = <_>::from(1i32);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::unnecessary_fallible_conversions)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
unnecessary_fallible_conversions = "allow"One run
cargo clippy -- -A clippy::unnecessary-fallible-conversionsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for calls to TryInto::try_into and TryFrom::try_from when their infallible counterparts
could be used.
Why is this bad?
In those cases, the TryInto and TryFrom trait implementation is a blanket impl that forwards
to Into or From, which always succeeds.
The returned Result<_, Infallible> requires error handling to get the contained value
even though the conversion can never fail.
Example
let _: Result<i64, _> = 1i32.try_into();
let _: Result<i64, _> = <_>::try_from(1i32);
Use from/into instead:
let _: i64 = 1i32.into();
let _: i64 = <_>::from(1i32);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- to_string_trait_impldirect implementation of `ToString`
- toplevel_ref_arg`ref` directly on a function parameter does not prevent taking ownership of the passed argument. Consider using a reference type instead
- trim_split_whitespacefound call to `str::trim` before `str::split_whitespace`
- unnecessary_foldthis `.fold` can be written more succinctly using another method
- unnecessary_lazy_evaluationsunnecessary closure used to substitute value for `Option::None`
- unnecessary_map_orthis `map_or` can be simplified
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?