Skip to content

#[must_use] on associated function definition when trait declaration does not have it should still trigger warning when calling function concretely #48486

Closed

Description

@Havvy

I expect the following code (Playpen) to emit a warning on the last line of main that the result of s.the_must_use_fn() must be used.

fn main () {
    let s = Struct;
    s.the_must_use_fn();
}

trait Trait {
    fn the_must_use_fn(&self) -> String;
}

struct Struct;

impl Trait for Struct {
    #[must_use]
    fn the_must_use_fn(&self) -> String { "".to_string() }
}

Ref #43302 as the tracking issue for this feature.

Activity

  1. zackmdavis commented on Feb 23, 2018

    @zackmdavis
    Contributor

    This is expected (or at least, known) behavior; the #[must_use] attribute needs to go on the function signature in the trait definition, not the implementation. I argue that the current behavior makes sense: the semantics of a trait method should be the same across implementations. We should document this better, though—thanks for starting on that!

  2. Havvy commented on Feb 23, 2018

    @Havvy
    ContributorAuthor

    Then there should be a warning when #[must_use] is on a trait method.

    But while it is known behavior, I didn't expect it. I expect that concretely calling a trait method should work the same as concretely calling an inherent method. Of course, when called generically, it should only look at the trait declaration.

  3. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Feb 27, 2018
  4. estebank commented on Nov 3, 2025

    @estebank
    Contributor

    Current output:

    warning: `#[must_use]` attribute cannot be used on trait methods in impl blocks
      --> src/main.rs:13:5
       |
    13 |     #[must_use]
       |     ^^^^^^^^^^^
       |
       = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
       = help: `#[must_use]` can be applied to data types, foreign functions, functions, inherent methods, provided trait methods, required trait methods, traits, and unions
       = note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default
    

    When setting it in the trait:

    warning: unused return value of `Trait::the_must_use_fn` that must be used
     --> src/main.rs:3:5
      |
    3 |     s.the_must_use_fn();
      |     ^^^^^^^^^^^^^^^^^^^
      |
      = note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default
    help: use `let _ = ...` to ignore the resulting value
      |
    3 |     let _ = s.the_must_use_fn();
      |     +++++++
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler 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