Email me

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-conversions

The [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.

Stylegroup
warndefault level
yesfixed by cargo clippy --fix
1.75.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ