[COMMITTED 30/77] gccrs: add unused doc comment lint

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Fri Aug 7 12:39:21 GMT 2026


From: Lucas Ly Ba <lucas.ly-ba@outlook.com>

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/unused-doc-comment_0.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
---
 .../checks/lints/unused/rust-unused-checker.cc     | 14 ++++++++++++++
 gcc/rust/checks/lints/unused/rust-unused-checker.h |  1 +
 gcc/testsuite/rust/compile/unused-doc-comment_0.rs | 10 ++++++++++
 3 files changed, 25 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/unused-doc-comment_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 643d262d2d5..0d091e6473a 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc
+++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
@@ -24,6 +24,7 @@
 
 #include "options.h"
 #include "rust-keyword-values.h"
+#include "rust-attribute-values.h"
 #include "rust-rib.h"
 
 namespace Rust {
@@ -333,5 +334,18 @@ UnusedChecker::visit (HIR::MatchExpr &expr)
   walk (expr);
 }
 
+void
+UnusedChecker::visit (HIR::LetStmt &stmt)
+{
+  for (auto &attr : stmt.get_outer_attrs ())
+    if (attr.get_path ().as_string () == Values::Attributes::DOC)
+      {
+	rust_warning_at (stmt.get_locus (), OPT_Wunused_variable,
+			 "unused doc comment");
+	break;
+      }
+  walk (stmt);
+}
+
 } // 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 2f2557ce3d8..46e012e7907 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-checker.h
+++ b/gcc/rust/checks/lints/unused/rust-unused-checker.h
@@ -51,6 +51,7 @@ private:
   virtual void visit (HIR::StructPatternFieldIdentPat &field) override;
   virtual void visit (HIR::MatchExpr &expr) override;
   virtual void visit (HIR::ExternBlock &block) override;
+  virtual void visit (HIR::LetStmt &stmt) override;
   virtual void visit_loop_label (HIR::LoopLabel &label) override;
 };
 } // namespace Analysis
diff --git a/gcc/testsuite/rust/compile/unused-doc-comment_0.rs b/gcc/testsuite/rust/compile/unused-doc-comment_0.rs
new file mode 100644
index 00000000000..5556563f37a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/unused-doc-comment_0.rs
@@ -0,0 +1,10 @@
+// { dg-additional-options "-frust-unused-check-2.0" }
+#![feature(no_core)]
+#![no_core]
+
+pub fn foo() {
+    /// useless
+    let _x = 5;
+// { dg-warning "unused doc comment" "" { target *-*-* } .-1 }
+    let _y = _x;
+}
-- 
2.50.1



More information about the Gcc-rust mailing list