[gcc r17-3125] gccrs: Fix duplicate unused assignment removals

Arthur Cohen via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:3b338a1ce871cbf36731d56f02d1d458243c1c23

commit r17-3125-g3b338a1ce871cbf36731d56f02d1d458243c1c23
Author: Aiman Najjar <[email protected]>
Date:   Fri Jul 31 22:32:03 2026 -0400

    gccrs: Fix duplicate unused assignment removals
    
    Fixes Rust-GCC/gccrs#4674
    
    gcc/rust/ChangeLog:
    
            * checks/lints/unused/rust-unused-context.cc
            (UnusedContext::remove_assign): Fix issue with duplicate
            removal of unused-assignment for the same id.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-4674.rs: New test.
    
    Signed-off-by: Aiman Najjar <[email protected]>

Diff:
---
 gcc/rust/checks/lints/unused/rust-unused-context.cc |  7 ++++++-
 gcc/testsuite/rust/compile/issue-4674.rs            | 15 +++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/checks/lints/unused/rust-unused-context.cc b/gcc/rust/checks/lints/unused/rust-unused-context.cc
index 9b26e01bccee..8a68a601acc3 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-context.cc
+++ b/gcc/rust/checks/lints/unused/rust-unused-context.cc
@@ -44,7 +44,12 @@ void
 UnusedContext::remove_assign (HirId id_def)
 {
   if (assigned_vars.find (id_def) != assigned_vars.end ())
-    assigned_vars[id_def].pop_back ();
+    {
+      assigned_vars[id_def].pop_back ();
+
+      if (assigned_vars[id_def].empty ())
+	assigned_vars.erase (id_def);
+    }
 }
 
 bool
diff --git a/gcc/testsuite/rust/compile/issue-4674.rs b/gcc/testsuite/rust/compile/issue-4674.rs
new file mode 100644
index 000000000000..f787c5c4bcfd
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4674.rs
@@ -0,0 +1,15 @@
+// { dg-additional-options "-frust-unused-check-2.0" }
+#![feature(no_core)]
+#![no_core]
+
+fn foo(mut n: i32) {
+    // { dg-warning "function is never used: .foo." "" { target *-*-* } .-1 }
+    if false {
+        n = 0i32;
+    }
+
+    if n > 0i32 {
+        let _ = 1i32 / n;
+    }
+}
+
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.