From: Lucas Ly Ba <[email protected]>

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]>
---
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/ff0229b955bcb49716cd4fb48bec584f4cfbd53f

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/4294

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

diff --git a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h 
b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
index ef43744a6..b578081b6 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 fffe61dd1..14e2df8da 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 000000000..8ae90d42f
--- /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 5dcc5978a..bc8198789 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 570c0d903..1048ff45a 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 }

base-commit: 941f9359546ea10c548c8f379bb0edfd9df3c52a
-- 
2.54.0

Reply via email to