Skip to content

#[must_use] on a trait method impl does nothing. #14202

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
theemathas opened this issue Feb 12, 2025 · 2 comments
Closed

#[must_use] on a trait method impl does nothing. #14202

theemathas opened this issue Feb 12, 2025 · 2 comments
Labels
A-lint Area: New lints

Comments

@theemathas
Copy link
Contributor

What it does

The lint should warn if #[must_use] is applied to a function in an impl Trait for Type { .... } block, and suggest applying it to the trait definition instead.

Advantage

As per the reference, #[must_use] in the impl Trait for Type block does absolutely nothing, and is therefore almost surely a mistake. On the other hand, #[must_use] in the trait definition applies to all implementations of the trait.

Drawbacks

No response

Example

trait Foo {
    fn bar(self) -> i32;
}
impl Foo for () {
    #[must_use]
    fn bar(self) -> i32 {
        1
    }
}

Could be written as:

trait Foo {
    #[must_use]
    fn bar(self) -> i32;
}
impl Foo for () {
    fn bar(self) -> i32 {
        1
    }
}
@theemathas theemathas added the A-lint Area: New lints label Feb 12, 2025
@y21
Copy link
Member

y21 commented Feb 12, 2025

Maybe this should be an extension to rustc's unused_attributes lint instead? That one is already used as a general lint for any kind of attribute used in some places where it doesn't do anything.

@samueltardieu
Copy link
Contributor

I agree with @y21. I've opened rust-lang/rust#136923 to extend the rustc lint. Closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants