Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wmmc88 opened this issue May 31, 2025 · 1 comment
Closed

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

wmmc88 opened this issue May 31, 2025 · 1 comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. L-unused_must_use Lint: unused_must_use

Comments

@wmmc88
Copy link

wmmc88 commented May 31, 2025

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

@wmmc88 wmmc88 added the C-bug Category: This is a bug. label May 31, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 31, 2025
@wmmc88 wmmc88 changed the title #[must_use] fires when generic type is unit type #[must_use] fires when generic type is unit type May 31, 2025
@Noratrieb
Copy link
Member

If you call a function with the #[must_use] attribute, its return value is unconditionally must_use. This is expected.

@Noratrieb Noratrieb added C-discussion Category: Discussion or questions that doesn't represent real issues. L-unused_must_use Lint: unused_must_use and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 1, 2025
@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. L-unused_must_use Lint: unused_must_use
Projects
None yet
Development

No branches or pull requests

3 participants