Skip to content

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

Open
@antonilol

Description

@antonilol

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

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-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions