Skip to content
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

FP deref_addrof #12319

Open
matthiaskrgr opened this issue Feb 20, 2024 · 4 comments
Open

FP deref_addrof #12319

matthiaskrgr opened this issue Feb 20, 2024 · 4 comments
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

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Feb 20, 2024

Summary

.

Lint Name

deref_addrof

Reproducer

I tried this code:

#![warn(clippy::deref_addrof)]

fn no_lint_ops() {
    let x = *&mut false |= true && return;
}

pub fn main() {}

I saw this happen:

warning: immediately dereferencing a reference
 --> src/main.rs:4:13
  |
4 |     let x = *&mut false |= true && return;
  |             ^^^^^^^^^^^ help: try: `false`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#deref_addrof
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::deref_addrof)]
  |         ^^^^^^^^^^^^^^^^^^^^

I expected to see this happen:
it no compily


The following errors were reported:
error[E0067]: invalid left-hand side of assignment
 --> src/main.rs:4:11
  |
4 |     false |= true && return;
  |     ----- ^^
  |     |
  |     cannot assign to this expression

error: aborting due to 1 previous error

Version

rustc 1.78.0-nightly (3246e7951 2024-02-19)
binary: rustc
commit-hash: 3246e79513cb89ddbfc0f21cb5a877e5b321dcc5
commit-date: 2024-02-19
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Additional Labels

No response

@matthiaskrgr matthiaskrgr 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 Feb 20, 2024
@alexis-langlet
Copy link

alexis-langlet commented Feb 23, 2024

From what I've seen, *&mut is doing a stack allocation and so the *&mut false is considered a variable

I was able to confirm the same thing happens using a variable

fn main() {
    let _ = false && return; // raise error
    let x = false; 
    let _ = x && return; // do not raise error
    let _ = *&mut false && return; // do not raise error
}

So I suppose that those checks are not done on variables and that the *&mut is "faking" a variable

@blyxyas
Copy link
Member

blyxyas commented Feb 23, 2024

I can confirm Alexis' theory. Here's the HIR for that *&mut false

Expr {
                            hir_id: HirId(DefId(0:3 ~ playground[be21]::no_lint_ops).3),
                            kind: Unary(
                                Deref,
                                Expr {
                                    hir_id: HirId(DefId(0:3 ~ playground[be21]::no_lint_ops).4),
                                    kind: AddrOf(
                                        Ref,
                                        Mut,
                                        Expr {
                                            hir_id: HirId(DefId(0:3 ~ playground[be21]::no_lint_ops).5),
                                            kind: Lit(
                                                Spanned {
                                                    node: Bool(
                                                        false,
                                                    ),
                                                    span: src/main.rs:5:19: 5:24 (#0),
                                                },
                                            ),
                                            span: src/main.rs:5:19: 5:24 (#0),
                                        },
                                    ),
                                    span: src/main.rs:5:14: 5:24 (#0),
                                },
                            ),
                            span: src/main.rs:5:13: 5:24 (#0),
                        },

I'll do some bugfixing.
@rustbot claim

@blyxyas
Copy link
Member

blyxyas commented Feb 23, 2024

Uff, work is pilling up and what I thought would be a very easy +1 line diff has become a bit more complex than anticipated.

I'll let a contributor handle it :D
@rustbot release-assignment

@bluss
Copy link
Member

bluss commented Mar 15, 2024

Also relevant, previous discussion of ndarray applications of *&mut: https://internals.rust-lang.org/t/assign-ops-papercut-with-custom-view-types/

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
Projects
None yet
Development

No branches or pull requests

4 participants