Skip to content

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

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

Open
awused opened this issue Jul 27, 2024 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way

Comments

@awused
Copy link

awused commented Jul 27, 2024

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

@awused awused added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 27, 2024
@rustbot rustbot added the I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way label Jul 27, 2024
@alex-semenyuk
Copy link
Member

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-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way
Projects
None yet
Development

No branches or pull requests

3 participants