Skip to content

#[must_use] fires when generic type is unit type #141802

Closed as not planned
Closed as not planned
@wmmc88

Description

@wmmc88

I tried this code:

use core::convert::Infallible;

pub trait UnwrapInfallible<T> {
    #[must_use]
    fn unwrap_infallible(self) -> T;
}

impl<T> UnwrapInfallible<T> for Result<T, Infallible> {
    #[inline]
    fn unwrap_infallible(self) -> T {
        match self {
            Ok(value) => value,
        }
    }
}

fn main() {
    let result: Result<(), Infallible> = Ok(());
    let result2: Result<u32, Infallible> = Ok(1);
    
    result.unwrap_infallible();
    let _u = result2.unwrap_infallible();
}

I expected to see this happen: no warnings
Instead, this happened: warning from unused () from result.unwrap_infallible():
warning: unused return value of UnwrapInfallible::unwrap_infallible that must be used

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-pc-windows-msvc
release: 1.86.0
LLVM version: 19.1.7

It's also in latest nightly: 2025-5-29

Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.L-unused_must_useLint: unused_must_use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions