Rollup merge of #156847 - sappho3:fix-suggestion-unused-variables-struct-pattern, r=JonathanBrouwer

Fix suggestion of unused variables with raw identifier in struct pattern

This MR fixes a broken lint suggestion that occurs when a struct pattern contains an unused variable written with a raw identifier.

In the following fragment, `r#move` is an unused variable. The compiler suggested to change it to `move: _` which doesn’t compile since move is a keyword. This change makes it so that the suggestion becomes `r#move: _`

```rust
struct Foo {
    r#move: u32
}

fn bar(foo: Foo) -> u32 {
    match foo {
         Foo { r#move } => 0
    }
}
```

r? JonathanBrouwer
This commit is contained in:
Jonathan Brouwer
2026-05-24 21:28:53 +02:00
committed by GitHub
5 changed files with 55 additions and 2 deletions
+1 -1
View File
@@ -249,7 +249,7 @@ pub(crate) enum UnusedVariableSugg {
shorthands: Vec<Span>,
#[suggestion_part(code = "_")]
non_shorthands: Vec<Span>,
name: Symbol,
name: String,
},
#[multipart_suggestion(
+1 -1
View File
@@ -1068,7 +1068,7 @@ fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
let sugg = if any_shorthand {
errors::UnusedVariableSugg::TryIgnore {
name,
name: name.to_ident_string(),
shorthands: introductions
.iter()
.filter_map(
@@ -0,0 +1,19 @@
//@ check-pass
//@ run-rustfix
#![warn(unused)]
#![allow(dead_code)]
struct Foo {
r#move: u32
}
fn main() {
let y = Foo { r#move: 3 };
let _ = match y {
Foo { r#move: _ } => 0 //~ WARNING unused variable: `r#move`
//~| HELP try ignoring the field
//~| SUGGESTION r#move: _
};
}
@@ -0,0 +1,19 @@
//@ check-pass
//@ run-rustfix
#![warn(unused)]
#![allow(dead_code)]
struct Foo {
r#move: u32
}
fn main() {
let y = Foo { r#move: 3 };
let _ = match y {
Foo { r#move } => 0 //~ WARNING unused variable: `r#move`
//~| HELP try ignoring the field
//~| SUGGESTION r#move: _
};
}
@@ -0,0 +1,15 @@
warning: unused variable: `r#move`
--> $DIR/unused-variable-with-raw-identifier-in-struct-pattern.rs:15:16
|
LL | Foo { r#move } => 0
| ^^^^^^ help: try ignoring the field: `r#move: _`
|
note: the lint level is defined here
--> $DIR/unused-variable-with-raw-identifier-in-struct-pattern.rs:4:9
|
LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
warning: 1 warning emitted