From: Lucas Ly Ba <[email protected]>

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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/2c7b222b96cb3dc63d104a16b65065fe21834829

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4628

 .../checks/lints/unused/rust-unused-checker.cc  | 17 +++++++++++++++++
 .../checks/lints/unused/rust-unused-checker.h   |  1 +
 .../compile/non-shorthand-field-patterns_0.rs   | 15 +++++++++++++++
 3 files changed, 33 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/non-shorthand-field-patterns_0.rs

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc 
b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index 6fd6c6efe..7d875daef 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 58e588022..055d49b19 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 000000000..364246e9d
--- /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 }
+    }
+}

base-commit: 6a15fac208959bb39a858581ccdacef8ca23684b
-- 
2.54.0

Reply via email to