[gcc r17-3088] gccrs: add unused doc comment lint

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

commit r17-3088-g75b8ea98d881658c589a612d1d3f5832e007b5e8
Author: Lucas Ly Ba <[email protected]>
Date:   Sat Jun 27 00:05:33 2026 +0200

    gccrs: add unused doc comment lint
    
    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 <[email protected]>

Diff:
---
 gcc/rust/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(+)

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index 643d262d2d5f..0d091e6473a1 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 2f2557ce3d89..46e012e79076 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 000000000000..5556563f37af
--- /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;
+}
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.