Skip to content

needless_pass_by_ref_mut: false negative, &mut self ignored #12589

Closed
@klensy

Description

@klensy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions