Skip to content

Wrong diagnostic label text due to text replacement #51244

Closed
@ytausky

Description

@ytausky

The following snippet:

fn main() {
    let ref my_ref @ _ = 0;
    *my_ref = 0
}

Produces the following output:

error[E0594]: cannot assign to immutable borrowed content `*my_ref`
 --> src/main.rs:3:5
  |
2 |     let ref my_ref @ _ = 0;
  |         -------------- consider changing this to `ref mut my_ref mut @ _`
3 |     *my_ref = 0
  |     ^^^^^^^^^^^ cannot borrow as mutable

Note that the label suggests invalid syntax. I believe this happens due to the text replacement in the following code:

ty::BindByReference(..) => {
let snippet = self.tcx.sess.codemap().span_to_snippet(let_span);
if let Ok(snippet) = snippet {
db.span_label(
let_span,
format!("consider changing this to `{}`",
snippet.replace("ref ", "ref mut "))
);
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions