rust-lang / rust-lang/rust-clippy
`needless_pass_by_ref_mut` should not trigger when values derived from the `&mut` are used in an unsafe block
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Using values derived from a &mut inside of an unsafe block can still trigger the needless_pass_by_ref_mut lint.
It seems like it was partially addressed in #11586, which should already be present in 1.80, but that only seems to cover direct use of the &mut in an unsafe block, not values derived from the &mut.
Lint Name
needless_pass_by_ref_mut
Reproducer
I tried this code:
#![warn(clippy::needless_pass_by_ref_mut)]
use std::ptr::NonNull;
struct MyStruct {
child: Option<NonNull<Self>>,
}
impl MyStruct {
fn do_thing(&mut self) {
if let Some(mut child) = self.child {
unsafe { child.as_mut() }.do_thing()
}
}
}
I saw this happen:
this argument is a mutable reference, but not used mutably
for further information visit https://rust-lang.github.io/rust-clippy/master/
index.html#needless_pass_by_ref_mut
`-W clippy::needless-pass-by-ref-mut` implied by `-W clippy::nursery`
to override `-W clippy::nursery` add `#[allow(clippy::
needless_pass_by_ref_mut)]` (clippy needless_pass_by_ref_mut)
I expected to see this happen:
Nothing in this case. The compiler can't know if my unsafe usage of child is safe to call from a shared reference. Switching it to a shared reference could cause unsoundness.
Version
rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7
Additional Labels
@rustbot label +I-suggestion-causes-bug
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided Rust example with the needless_pass_by_mut lint enabled, then trace the lint's handling of values derived from an &mut inside an unsafe block. Done means the reproducer no longer emits a warning while ordinary needless mutable references remain diagnosed, with a regression test covering the case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100