From 8604f001a96a05f58cadfb4695b39e9458ac3f1e Mon Sep 17 00:00:00 2001 From: Sappho de Nooij Date: Fri, 22 May 2026 10:40:14 +0200 Subject: [PATCH] Fix suggestion of unused variables with raw identifier in struct pattern This commit fixes a broken suggestion that occurs when a struct pattern contains an unused variable written with a raw identifier. --- compiler/rustc_mir_transform/src/errors.rs | 2 +- compiler/rustc_mir_transform/src/liveness.rs | 2 +- ...ith-raw-identifier-in-struct-pattern.fixed | 19 +++++++++++++++++++ ...e-with-raw-identifier-in-struct-pattern.rs | 19 +++++++++++++++++++ ...th-raw-identifier-in-struct-pattern.stderr | 15 +++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.fixed create mode 100644 tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.rs create mode 100644 tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.stderr diff --git a/compiler/rustc_mir_transform/src/errors.rs b/compiler/rustc_mir_transform/src/errors.rs index 39c85489f939..9e605479bd0e 100644 --- a/compiler/rustc_mir_transform/src/errors.rs +++ b/compiler/rustc_mir_transform/src/errors.rs @@ -290,7 +290,7 @@ pub(crate) enum UnusedVariableSugg { shorthands: Vec, #[suggestion_part(code = "_")] non_shorthands: Vec, - name: Symbol, + name: String, }, #[multipart_suggestion( diff --git a/compiler/rustc_mir_transform/src/liveness.rs b/compiler/rustc_mir_transform/src/liveness.rs index a1b2a2853c0b..c449cf686739 100644 --- a/compiler/rustc_mir_transform/src/liveness.rs +++ b/compiler/rustc_mir_transform/src/liveness.rs @@ -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( diff --git a/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.fixed b/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.fixed new file mode 100644 index 000000000000..9e188cfdcc0e --- /dev/null +++ b/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.fixed @@ -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: _ + }; +} diff --git a/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.rs b/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.rs new file mode 100644 index 000000000000..514ba4f7df41 --- /dev/null +++ b/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.rs @@ -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: _ + }; +} diff --git a/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.stderr b/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.stderr new file mode 100644 index 000000000000..0f9660565161 --- /dev/null +++ b/tests/ui/lint/unused/unused-variable-with-raw-identifier-in-struct-pattern.stderr @@ -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 +