[gccrs,COMMIT] gccrs: add unused doc comment lint

Message ID 20260708092916.3100-1-gerris.rs@gmail.com
State New
Headers
Series [gccrs,COMMIT] gccrs: add unused doc comment lint |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 pending Build did not complete; will be retriggered

Commit Message

gerris.rs@gmail.com July 8, 2026, 9:29 a.m. UTC
  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>
---
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/25ab7ba2877cec8bb831ddfeea9ecb72ef23c9d5

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

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


base-commit: 864defecb6c0faf055005b48a24914651ef5c727
  

Patch

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index 643d262d2..0d091e647 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 2f2557ce3..46e012e79 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 000000000..5556563f3
--- /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;
+}