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

needless_pass_by_ref_mut: false negative, &mut self ignored #12589

Closed
klensy opened this issue Mar 29, 2024 · 3 comments · Fixed by #12693
Closed

needless_pass_by_ref_mut: false negative, &mut self ignored #12589

klensy opened this issue Mar 29, 2024 · 3 comments · Fixed by #12693
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@klensy
Copy link
Contributor

klensy commented Mar 29, 2024

Summary

Looks like mutable self refs ignored in methods.

Discovered while working on rust-lang/rust#123188

Checked with clippy from playground 0.1.79 (2024-03-27 c9f8f34)

https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=1eb816feca6371e80e4b27f59b21174f

see also #9591

Lint Name

needless_pass_by_ref_mut

Reproducer

I tried this code:

#![warn(clippy::needless_pass_by_ref_mut)]

fn main() {}

struct Foo{
    x: u8
}

impl Foo{
    fn foo(&mut self)->u8{
        self.x * 2
    }

    fn bar(&mut self, y: &mut u8)->u8{
        self.x * *y
    }
    
    fn baz(&self, y: &mut u8)->u8{
        self.x * *y
    }
}

I expected to see this happen:
Should warn about unused &mut self in foo and bar
Instead, this happened:
No warn for &mut self, only for y:

warning: this argument is a mutable reference, but not used mutably
  --> src/main.rs:14:26
   |
14 |     fn bar(&mut self, y: &mut u8)->u8{
   |                          ^^^^^^^ help: consider changing to: `&u8`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::needless_pass_by_ref_mut)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: this argument is a mutable reference, but not used mutably
  --> src/main.rs:18:22
   |
18 |     fn baz(&self, y: &mut u8)->u8{
   |                      ^^^^^^^ help: consider changing to: `&u8`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut

Version

No response

@klensy klensy added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Mar 29, 2024
@klensy
Copy link
Contributor Author

klensy commented Mar 29, 2024

Well, self args explicitly ignored why?

if is_self(arg) {
return true;
}

@klensy
Copy link
Contributor Author

klensy commented Mar 29, 2024

Found discussion at #10900 (comment), lint description should probably note, that &mut self args currently not linted.

@GuillaumeGomez
Copy link
Member

I'll add a note for it and a setting to allow to bypass this default behaviour.

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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
2 participants