Skip to content

return_self_not_must_use contradicts double_must_use for #[must_use]-types #8140

Closed
@fmease

Description

@fmease

Summary

With #![warn(clippy::return_self_not_must_use)], Clippy wants me to mark a method returning Self with #[must_use] even if Self is a #[must_use]-type.

A #[must_use]-type renders any methods returning Self #[must_use] already. Adding #[must_use] on the method is duplication (the method is transitively must-use). Doing so anyway rightly triggers the lint double_must_use. Hence, return_self_not_must_use contradicts double_must_use.

Lint Name

return_self_not_must_use

Reproducer

When I run the following (“code A”):

#![warn(clippy::return_self_not_must_use)]
#![warn(clippy::double_must_use)]

#[must_use]
pub struct M;

impl M {
    pub fn f(self) -> Self {
        self
    }
}

Clippy outputs:

warning: missing `#[must_use]` attribute on a method returning `Self`
  --> src/lib.rs:8:5
   |
8  | /     pub fn f(self) -> Self {
9  | |         self
10 | |     }
   | |_____^
   |
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(clippy::return_self_not_must_use)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#return_self_not_must_use

Suggesting adding #[must_use] on method f. However, when I do this (“code B”):

#![warn(clippy::return_self_not_must_use)]
#![warn(clippy::double_must_use)]

#[must_use]
pub struct M;

impl M {
    #[must_use]
    pub fn f(self) -> Self {
        self
    }
}

Then Clippy is still not happy giving the warning seen below:

warning: this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`
 --> src/lib.rs:9:5
  |
9 |     pub fn f(self) -> Self {
  |     ^^^^^^^^^^^^^^^^^^^^^^
  |
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![warn(clippy::double_must_use)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^
  = help: either add some descriptive text or remove the attribute
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_must_use

I expected to see this happen:

No warnings being emitted by Clippy for Code A: M is marked #[must_use] and thus method f transitively becomes must-use.

Version

rustc 1.59.0-nightly (7abab1efb 2021-12-17)
binary: rustc
commit-hash: 7abab1efb21617ba6845fa86328dffa16cfcf1dc
commit-date: 2021-12-17
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions