Skip to content

needless_pass_by_ref_mut should not trigger when values derived from the &mut are used in an unsafe block #13172

Open
@awused

Description

@awused

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions