Skip to content
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

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

Closed
fmease opened this issue Dec 18, 2021 · 4 comments · Fixed by #8143
Closed

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

fmease opened this issue Dec 18, 2021 · 4 comments · Fixed by #8143
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@fmease
Copy link
Member

fmease commented Dec 18, 2021

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

@fmease fmease added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 18, 2021
@fmease
Copy link
Member Author

fmease commented Dec 18, 2021

cc @GuillaumeGomez #8071

@GuillaumeGomez
Copy link
Member

Ah indeed, I'll add a check to ensure a must_use method doesn't emit this warning.

@bors bors closed this as completed in 25e90ec Dec 18, 2021
bors bot added a commit to foresterre/cargo-msrv that referenced this issue Dec 20, 2021
218: make 'builder' 'must_use' r=foresterre a=danieleades

make the `ConfigBuilder` 'must_use'.

There's no valid reason ever to not use a builder struct, hence it makes sense to mark it as `#[must_use]`

note that there's actually a false positive in clippy right now which is related to this - rust-lang/rust-clippy#8140

Co-authored-by: Daniel Eades <danieleades@hotmail.com>
@danieleades
Copy link

this is still failing all my builds. any idea when the fix will come through the pipeline?

@xFrednet
Copy link
Member

The changes have been synced about 6 hours ago, it should be fixed with the next sync :)

foresterre pushed a commit to foresterre/cargo-msrv that referenced this issue Aug 1, 2022
218: make 'builder' 'must_use' r=foresterre a=danieleades

make the `ConfigBuilder` 'must_use'.

There's no valid reason ever to not use a builder struct, hence it makes sense to mark it as `#[must_use]`

note that there's actually a false positive in clippy right now which is related to this - rust-lang/rust-clippy#8140

Co-authored-by: Daniel Eades <danieleades@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
4 participants