https://gcc.gnu.org/g:e4d8913b6b1d031621f10b5e47ebe00bfd674c6f
commit r17-2236-ge4d8913b6b1d031621f10b5e47ebe00bfd674c6f Author: Lucas Ly Ba <[email protected]> Date: Sat Jun 27 00:39:10 2026 +0200 gccrs: add non shorthand field patterns lint gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): New. * checks/lints/unused/rust-unused-checker.h (UnusedChecker::visit): New. gcc/testsuite/ChangeLog: * rust/compile/non-shorthand-field-patterns_0.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]> Diff: --- gcc/rust/checks/lints/unused/rust-unused-checker.cc | 17 +++++++++++++++++ gcc/rust/checks/lints/unused/rust-unused-checker.h | 1 + .../rust/compile/non-shorthand-field-patterns_0.rs | 15 +++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc index 6fd6c6efe1ca..7d875daef339 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc @@ -198,5 +198,22 @@ UnusedChecker::visit_loop_label (HIR::LoopLabel &label) "unused label %qs", lifetime.to_string ().c_str ()); } +void +UnusedChecker::visit (HIR::StructPatternFieldIdentPat &field) +{ + auto &pattern = field.get_pattern (); + if (pattern.get_pattern_type () == HIR::Pattern::PatternType::IDENTIFIER) + { + auto &ident = static_cast<HIR::IdentifierPattern &> (pattern); + if (!ident.has_subpattern () + && ident.get_identifier ().as_string () + == field.get_identifier ().as_string ()) + rust_warning_at (field.get_locus (), OPT_Wunused_variable, + "the %qs in this pattern is redundant", + (field.get_identifier ().as_string () + ":").c_str ()); + } + walk (field); +} + } // namespace Analysis } // namespace Rust diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.h b/gcc/rust/checks/lints/unused/rust-unused-checker.h index 58e588022c60..055d49b193e1 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.h +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.h @@ -48,6 +48,7 @@ private: virtual void visit (HIR::Function &fct) override; virtual void visit (HIR::Module &mod) override; virtual void visit (HIR::LifetimeParam &lft) override; + virtual void visit (HIR::StructPatternFieldIdentPat &field) override; virtual void visit_loop_label (HIR::LoopLabel &label) override; }; } // namespace Analysis diff --git a/gcc/testsuite/rust/compile/non-shorthand-field-patterns_0.rs b/gcc/testsuite/rust/compile/non-shorthand-field-patterns_0.rs new file mode 100644 index 000000000000..364246e9d557 --- /dev/null +++ b/gcc/testsuite/rust/compile/non-shorthand-field-patterns_0.rs @@ -0,0 +1,15 @@ +// { dg-additional-options "-frust-unused-check-2.0" } +#![feature(no_core)] +#![no_core] + +pub struct Point { + pub x: i32, + pub y: i32, +} + +pub fn foo(p: Point) -> i32 { + match p { + Point { x: x, y: _y } => x, +// { dg-warning "in this pattern is redundant" "" { target *-*-* } .-1 } + } +}
