Skip to content

FP needless-pass-by-ref-mut with lifetimes #16267

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::needless-pass-by-ref-mut

this code:

mod issue11819 {
    fn takes_into_iter(_: impl IntoIterator<Item = String>) {}

    pub struct MyStruct<T> {
        my_field: T,
    }

    impl<T> MyStruct<T> {
        pub fn with_ref<'a>(&'a mut self)
        where
            &'a T: IntoIterator<Item = String>,
        {
            takes_into_iter(self.my_field.into_iter());
            //~^ useless_conversion
        }

        pub fn with_ref_mut<'a>(&'a mut self)
        where
            &'a mut T: IntoIterator<Item = String>,
        {
            takes_into_iter(self.my_field.into_iter());
            //~^ useless_conversion
        }

        pub fn with_deref<Y>(&mut self)
        where
            T: std::ops::Deref<Target = Y>,
            Y: IntoIterator<Item = String> + Copy,
        {
            takes_into_iter(self.my_field.into_iter());
            //~^ useless_conversion
        }
}
}
fn main() {}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.kGLza1WPRM6O/icemaker_clippyfix_tempdir.lVXtr8bw7nRI/_a)
warning: this argument is a mutable reference, but not used mutably
 --> src/main.rs:9:29
  |
9 |         pub fn with_ref<'a>(&'a mut self)
  |                             ^^^^^^^^^^^^ help: consider changing to: `&self`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
  = note: requested on the command line with `--force-warn clippy::needless-pass-by-ref-mut`

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

warning: `_a` (bin "_a") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s

However after applying these diagnostics, the resulting code:

mod issue11819 {
    fn takes_into_iter(_: impl IntoIterator<Item = String>) {}

    pub struct MyStruct<T> {
        my_field: T,
    }

    impl<T> MyStruct<T> {
        pub fn with_ref<'a>(&self)
        where
            &'a T: IntoIterator<Item = String>, T: 'a
        {
            takes_into_iter(self.my_field.into_iter());
            //~^ useless_conversion
        }

        pub fn with_ref_mut<'a>(&'a mut self)
        where
            &'a mut T: IntoIterator<Item = String>,
        {
            takes_into_iter(self.my_field.into_iter());
            //~^ useless_conversion
        }

        pub fn with_deref<Y>(&self)
        where
            T: std::ops::Deref<Target = Y>,
            Y: IntoIterator<Item = String> + Copy,
        {
            takes_into_iter(self.my_field.into_iter());
            //~^ useless_conversion
        }
}
}
fn main() {}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.kGLza1WPRM6O/icemaker_clippyfix_tempdir.lVXtr8bw7nRI/_a)
error: lifetime may not live long enough
  --> src/main.rs:13:29
   |
 9 |         pub fn with_ref<'a>(&self)
   |                         --  - let's call the lifetime of this reference `'1`
   |                         |
   |                         lifetime `'a` defined here
...
13 |             takes_into_iter(self.my_field.into_iter());
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'a`

error: could not compile `_a` (bin "_a" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a") due to 1 previous error

Version:

rustc 1.94.0-nightly (806c2a35d 2025-12-19)
binary: rustc
commit-hash: 806c2a35dcc1db7867864fea0a7a65554a5238b7
commit-date: 2025-12-19
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.8

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions