rust-lang / rust-lang/rust-clippy
Unwrap_used false positive with Infallible return value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Unwrapping a function that returns core::convert::Infallible or std::convert::Infallible results in a false positive clippy::unwrap_used lint.
May be related to #11245
Lint Name
unwrap_used
Reproducer
First I tried this, in a function that returns Result<Svg, Error> where Error is a custom struct based Error type.
let output = Svg::from_str(&svg_string)?;
Which resulted in a error:
error[E0277]: `?` couldn't convert the error to `error::Error`
--> crates/cdm_core/src/datatypes/project_types/mounting_rail.rs:106:48
|
106 | let output = Svg::from_str(&svg_string)?;
| --------------------------^ the trait `std::convert::From<std::convert::Infallible>` is not implemented for `error::Error`
| |
| this can't be annotated with `?` because it has type `Result<_, std::convert::Infallible>`
|
note: `error::Error` needs to implement `From<std::convert::Infallible>`
--> crates/cdm_core/src/error.rs:11:1
Fair enough: I tried this to work around the issue:
let output = Svg::from_str(&svg_string).unwrap();
Which triggered the following lints:
// at the call site
clippy::unwrap_used
// on the containing function
clippy::missing_panics_doc
then I tried adding an #[expect]:
#[expect(clippy::unwrap_used, reason = "this function in infalliable")]
let output = Svg::from_str(&svg_string).unwrap();
which triggered the following lints:
// at the call site
clippy::unwrap_used
// on the containing function
clippy::missing_panics_doc
// on the expect attribute
this lint expectation is unfulfilled
I expected to see this happen:
No warning or error while unwrapping the return value of a function that returns core::convert::Infallible or std::convert::Infallible per the fixes in #11245
FromStr definition for reference. Infallible is core::convert::Infallible here, but I have also tried std::convert::Infallible with no changes.
impl FromStr for Svg {
type Err = Infallible;
//TODO: validation
/// Creates a `Svg` from a string representation of an Svg. No validation is performed
/// currently.
#[inline]
fn from_str(text: &str) -> Result<Self, Self::Err> {
Ok(Self {
svg_data: text.to_owned(),
filepath: None,
})
}
}
Version
rustc 1.94.0 (4a4ef493e 2026-03-02)
binary: rustc
commit-hash: 4a4ef493e3a1488c6e321570238084b38948f6db
commit-date: 2026-03-02
host: x86_64-unknown-linux-gnu
release: 1.94.0
LLVM version: 21.1.8
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the clippy::unwrap_used lint implementation and its existing tests, using the provided Infallible reproducer as the regression case. Confirm that unwrapping a value whose error type is core::convert::Infallible or std::convert::Infallible does not trigger the lint, while ordinary fallible results still do.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100