https://gcc.gnu.org/g:bbed1c020be1bcc3d255a6e2a0e279f150394a91

commit r17-3109-gbbed1c020be1bcc3d255a6e2a0e279f150394a91
Author: Lucas Ly Ba <[email protected]>
Date:   Tue Nov 18 16:02:27 2025 +0000

    gccrs: refactor dead code lint
    
    This patch is simple, it only moves the check of unused static items
    and unused const items into the dead-code scan visitor. The static
    item naming lint stays in the unused checker.
    
    gcc/rust/ChangeLog:
    
            * checks/lints/rust-lint-scan-deadcode.h
            (ScanDeadcode::visit(HIR::ConstantItem))
            (ScanDeadcode::visit(HIR::StaticItem)): New.
            * checks/lints/unused/rust-unused-checker.cc
            (UnusedChecker::visit(HIR::ConstantItem)): Remove unused-item check,
            keep visibility lint.
            (UnusedChecker::visit(HIR::StaticItem)): Remove unused-item check,
            keep naming lint.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/static_item_0.rs: Change warning description.
            * rust/compile/static-mut-refs_0.rs: Make static public.
            * rust/compile/const_item_0.rs: New test.
    
    Signed-off-by: Lucas Ly Ba <[email protected]>

Diff:
---
 gcc/rust/checks/lints/rust-lint-scan-deadcode.h    | 29 ++++++++++++++++++++++
 .../checks/lints/unused/rust-unused-checker.cc     | 14 -----------
 gcc/testsuite/rust/compile/const_item_0.rs         |  6 +++++
 gcc/testsuite/rust/compile/static-mut-refs_0.rs    |  2 +-
 gcc/testsuite/rust/compile/static_item_0.rs        |  2 +-
 5 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h 
b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
index ef43744a623d..b578081b6eb7 100644
--- a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
+++ b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
@@ -19,6 +19,7 @@
 #ifndef RUST_HIR_SCAN_DEADCODE
 #define RUST_HIR_SCAN_DEADCODE
 
+#include "options.h"
 #include "rust-hir-full-decls.h"
 #include "rust-hir-map.h"
 #include "rust-lint-marklive.h"
@@ -136,6 +137,34 @@ public:
       item->accept_vis (*this);
   }
 
+  void visit (HIR::ConstantItem &item) override
+  {
+    if (!flag_unused_check_2_0)
+      return;
+    std::string var_name = item.get_identifier ().as_string ();
+    bool starts_with_under_score = var_name.at (0) == '_';
+    HirId hirId = item.get_mappings ().get_hirid ();
+    if (should_warn (hirId) && !item.get_visibility ().is_public ()
+       && !starts_with_under_score)
+      rust_warning_at (item.get_locus (), OPT_Wunused_variable,
+                      "deadcode const item %qs",
+                      item.get_identifier ().as_string ().c_str ());
+  }
+
+  void visit (HIR::StaticItem &item) override
+  {
+    if (!flag_unused_check_2_0)
+      return;
+    std::string var_name = item.get_identifier ().as_string ();
+    bool starts_with_under_score = var_name.at (0) == '_';
+    HirId hirId = item.get_mappings ().get_hirid ();
+    if (should_warn (hirId) && !item.get_visibility ().is_public ()
+       && !starts_with_under_score)
+      rust_warning_at (item.get_locus (), OPT_Wunused_variable,
+                      "deadcode static item %qs",
+                      item.get_identifier ().as_string ().c_str ());
+  }
+
 private:
   std::set<HirId> live_symbols;
   Resolver::Resolver *resolver;
diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc 
b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index fffe61dd1292..14e2df8dac87 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc
+++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
@@ -55,14 +55,6 @@ void
 UnusedChecker::visit (HIR::ConstantItem &item)
 {
   std::string var_name = item.get_identifier ().as_string ();
-  auto id = item.get_mappings ().get_hirid ();
-  if (!unused_context.is_variable_used (id) && var_name[0] != '_')
-    rust_warning_at (item.get_locus (), OPT_Wunused_variable,
-                    "unused variable %qs",
-                    item.get_identifier ().as_string ().c_str ());
-
-  // The unused_visibilities lint: a visibility qualifier on a `const _` item
-  // has no effect.
   if (var_name == "_" && item.get_visibility ().is_public ())
     rust_warning_at (item.get_locus (), OPT_Wunused_variable,
                     "visibility qualifier on a %<const _%> item is unused");
@@ -72,12 +64,6 @@ void
 UnusedChecker::visit (HIR::StaticItem &item)
 {
   std::string var_name = item.get_identifier ().as_string ();
-  auto id = item.get_mappings ().get_hirid ();
-  if (!unused_context.is_variable_used (id) && var_name[0] != '_')
-    rust_warning_at (item.get_locus (), OPT_Wunused_variable,
-                    "unused variable %qs",
-                    item.get_identifier ().as_string ().c_str ());
-
   if (!std::all_of (var_name.begin (), var_name.end (), [] (unsigned char c) {
        return ISUPPER (c) || ISDIGIT (c) || c == '_';
       }))
diff --git a/gcc/testsuite/rust/compile/const_item_0.rs 
b/gcc/testsuite/rust/compile/const_item_0.rs
new file mode 100644
index 000000000000..8ae90d42f9ae
--- /dev/null
+++ b/gcc/testsuite/rust/compile/const_item_0.rs
@@ -0,0 +1,6 @@
+// { dg-additional-options "-frust-unused-check-2.0" }
+#![feature(no_core)]
+#![no_core]
+
+const A: usize = 1;
+// { dg-warning "deadcode const item .A." "" { target *-*-* } .-1 }
diff --git a/gcc/testsuite/rust/compile/static-mut-refs_0.rs 
b/gcc/testsuite/rust/compile/static-mut-refs_0.rs
index 5dcc5978a744..bc8198789a08 100644
--- a/gcc/testsuite/rust/compile/static-mut-refs_0.rs
+++ b/gcc/testsuite/rust/compile/static-mut-refs_0.rs
@@ -5,7 +5,7 @@
 #[lang = "sized"]
 pub trait Sized {}
 
-static mut S: i32 = 0;
+pub static mut S: i32 = 0;
 
 pub unsafe fn f() {
     let _y = &S;
diff --git a/gcc/testsuite/rust/compile/static_item_0.rs 
b/gcc/testsuite/rust/compile/static_item_0.rs
index 570c0d903b0b..1048ff45a05d 100644
--- a/gcc/testsuite/rust/compile/static_item_0.rs
+++ b/gcc/testsuite/rust/compile/static_item_0.rs
@@ -3,4 +3,4 @@
 #![no_core]
 
 static TEST: usize = 1;
-// { dg-warning "unused variable .TEST." "" { target *-*-* } .-1 }
+// { dg-warning "deadcode static item .TEST." "" { target *-*-* } .-1 }

Reply via email to