Skip to content

FP borrow_deref_ref #14934

Closed
Closed
@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Reproducer

I tried this code:

#![allow(clippy::toplevel_ref_arg, clippy::uninlined_format_args)]
fn main() {
    let x: &'static str = "x";

    {
        let y = "y".to_string();
        let ref mut x = &*x;
        *x = &*y;
    }

    println!("{:?}", x);
}

suggests

warning: deref on an immutable reference
 --> src/main.rs:7:25
  |
7 |         let ref mut x = &*x;
  |                         ^^^ help: if you would like to reborrow, try removing `&*`: `x`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref
  = note: `#[warn(clippy::borrow_deref_ref)]` on by default

however this code

#![allow(clippy::toplevel_ref_arg, clippy::uninlined_format_args)]
fn main() {
    let x: &'static str = "x";

    {
        let y = "y".to_string();
        let ref mut x = x;
        *x = &*y;
    }

    println!("{:?}", x);
}

does not compile:

error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
 --> src/main.rs:7:13
  |
7 |         let ref mut x = x;
  |             ^^^^^^^^^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
3 |     let mut x: &'static str = "x";
  |         +++

error[E0597]: `y` does not live long enough
 --> src/main.rs:8:16
  |
3 |     let x: &'static str = "x";
  |            ------------ type annotation requires that `y` is borrowed for `'static`
...
6 |         let y = "y".to_string();
  |             - binding `y` declared here
7 |         let ref mut x = x;
8 |         *x = &*y;
  |                ^ borrowed value does not live long enough
9 |     }
  |     - `y` dropped here while still borrowed

Version


Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions