Skip to content

needless_borrow emitted on expression that changes types when & is removed #14352

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
antonilol opened this issue Mar 4, 2025 · 2 comments
Open
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-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@antonilol
Copy link
Contributor

antonilol commented Mar 4, 2025

Summary

The error after clippy --fix, this should compile in my opinion. It does compile without the curly braces around guard. It is not a clippy false positive if this is considered a rustc bug.

error[E0308]: mismatched types
 --> src/main.rs:6:17
  |
6 |     function(&{ guard });
  |                 ^^^^^ expected `()`, found `Ref<'_, ()>`
  |
  = note: expected unit type `()`
                found struct `std::cell::Ref<'_, ()>`

Lint Name

needless_borrow

Reproducer

I tried this code:

fn function(_: &()) {}

fn main() {
    let refcell = core::cell::RefCell::new(());
    let guard = refcell.borrow();
    function(&&{ guard }); // two &
}

I saw this happen:

"failed to automatically apply fixes suggested by rustc"

I expected to see this happen:

I expected the result after clippy --fix to compile

fn function(_: &()) {}

fn main() {
    let refcell = core::cell::RefCell::new(());
    let guard = refcell.borrow();
    function(&{ guard }); // one &
}

Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Additional Labels

@rustbot label I-suggestion-causes-error

@antonilol antonilol 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 Mar 4, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Mar 4, 2025
@antonilol
Copy link
Contributor Author

related: rust-lang/rust#23014

@samueltardieu
Copy link
Contributor

This is indeed a rustc bug, but we could try not to suggest code that won't compile (either by not triggering the lint, or by finding a better suggestion).

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-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants