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

@Havvy

Description

@Havvy
Contributor

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

zackmdavis

zackmdavis commented on Feb 23, 2018

@zackmdavis
Member

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!

Havvy

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.

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
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

        Participants

        @Havvy@zackmdavis@pietroalbini

        Issue actions

          #[must_use] on associated function definition when trait declaration does not have it should still trigger warning when calling function concretely · Issue #48486 · rust-lang/rust