Skip to content

#[must_use] does not work for nested structures #39524

Closed
@sunjay

Description

@sunjay

When #[must_use] is applied to a structure that is nested, the linter no longer picks up when that value is unused.

This is related to: #26291 and #26281

Looking at those issues, there seem to be a lot of cases where #[must_use] does not work. This is unfortunate because it is actually an incredibly useful annotation that definitely has applications past just the Result enum.

Accidentally relying on it too much can have you make mistakes you otherwise would have corrected had the warning been a bit smarter. I consider this a bug because #[must_use] implies that you must use that value no matter what; regardless of what form it is returned in.

I tried this code: (Rust Playground: https://is.gd/vCLZKq)

#[must_use]
#[derive(Debug)]
struct MustUse;

struct Foo {
    must: MustUse,
}

impl Foo {
    fn bar(&self) {
        println!("{:?}", self.must);
    }
}

fn must() -> Foo {
    Foo {must: MustUse}
}

fn main() {
    must().bar();
    must();
}

I expected to see this happen: A warning about the MustUse structure not being used

Instead, this happened: The code compiled and ran with no errors

Meta

rustc --version --verbose: stable, nightly, etc.

Backtrace: not applicable

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions