https://gcc.gnu.org/g:3b972ec2fbdf049c561be3875cfda596cecc74dc

commit r17-3100-g3b972ec2fbdf049c561be3875cfda596cecc74dc
Author: Lucas Ly Ba <[email protected]>
Date:   Sat Jun 27 23:47:04 2026 +0200

    gccrs: add static mut refs lint
    
    Warn on taking a reference to a mutable static, which is discouraged as
    it can easily lead to undefined behaviour.
    
    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/static-mut-refs_0.rs: New test.
    
    Signed-off-by: Lucas Ly Ba <[email protected]>

Diff:
---
 gcc/rust/checks/lints/unused/rust-unused-checker.cc | 20 ++++++++++++++++++++
 gcc/rust/checks/lints/unused/rust-unused-checker.h  |  1 +
 gcc/testsuite/rust/compile/static-mut-refs_0.rs     | 13 +++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc 
b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index 0d091e6473a1..fffe61dd1292 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc
+++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
@@ -347,5 +347,25 @@ UnusedChecker::visit (HIR::LetStmt &stmt)
   walk (stmt);
 }
 
+void
+UnusedChecker::visit (HIR::BorrowExpr &expr)
+{
+  // The static_mut_refs lint: taking a reference to a mutable static is
+  // discouraged as it can easily lead to undefined behaviour.
+  NodeId ast_node_id = expr.get_expr ().get_mappings ().get_nodeid ();
+  if (auto def
+      = nr_context.lookup (ast_node_id, Resolver2_0::Namespace::Values))
+    if (auto id = mappings.lookup_node_to_hir (*def))
+      if (auto item = mappings.lookup_hir_item (*id))
+       if (item.value ()->get_item_kind () == HIR::Item::ItemKind::Static)
+         {
+           auto &static_item = static_cast<HIR::StaticItem &> (*item.value ());
+           if (static_item.is_mut ())
+             rust_warning_at (expr.get_locus (), OPT_Wunused,
+                              "creating a reference to a mutable static");
+         }
+  walk (expr);
+}
+
 } // 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 46e012e79076..161f59b8a662 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-checker.h
+++ b/gcc/rust/checks/lints/unused/rust-unused-checker.h
@@ -52,6 +52,7 @@ private:
   virtual void visit (HIR::MatchExpr &expr) override;
   virtual void visit (HIR::ExternBlock &block) override;
   virtual void visit (HIR::LetStmt &stmt) override;
+  virtual void visit (HIR::BorrowExpr &expr) override;
   virtual void visit_loop_label (HIR::LoopLabel &label) override;
 };
 } // namespace Analysis
diff --git a/gcc/testsuite/rust/compile/static-mut-refs_0.rs 
b/gcc/testsuite/rust/compile/static-mut-refs_0.rs
new file mode 100644
index 000000000000..5dcc5978a744
--- /dev/null
+++ b/gcc/testsuite/rust/compile/static-mut-refs_0.rs
@@ -0,0 +1,13 @@
+// { dg-additional-options "-frust-unused-check-2.0" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+static mut S: i32 = 0;
+
+pub unsafe fn f() {
+    let _y = &S;
+// { dg-warning "reference to a mutable static" "" { target *-*-* } .-1 }
+}

Reply via email to