[COMMITTED 31/41] gccrs: Do not warn on unused function with "_" prefix

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Fri Mar 20 17:30:20 GMT 2026


From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

The compiler should not emit any unused function warning on function name
prefixed with an underscore.

gcc/rust/ChangeLog:

	* checks/lints/rust-lint-scan-deadcode.h: Add additional condition
	to prevent warning emission. Check for underscore prefix.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3947.rs: Do not expect a warning on this
	underscore prefixed function.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
---
 gcc/rust/checks/lints/rust-lint-scan-deadcode.h | 5 ++++-
 gcc/testsuite/rust/compile/issue-3947.rs        | 1 -
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
index c791579867e..ef43744a623 100644
--- a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
+++ b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
@@ -51,7 +51,10 @@ public:
   void visit (HIR::Function &function) override
   {
     HirId hirId = function.get_mappings ().get_hirid ();
-    if (should_warn (hirId) && !function.get_visibility ().is_public ())
+    auto starts_with_underscore
+      = function.get_function_name ().as_string ().rfind ('_', 0) == 0;
+    if (should_warn (hirId) && !function.get_visibility ().is_public ()
+	&& !starts_with_underscore)
       {
 	if (mappings.is_impl_item (hirId))
 	  {
diff --git a/gcc/testsuite/rust/compile/issue-3947.rs b/gcc/testsuite/rust/compile/issue-3947.rs
index 4d33c632c3f..b5219e7603d 100644
--- a/gcc/testsuite/rust/compile/issue-3947.rs
+++ b/gcc/testsuite/rust/compile/issue-3947.rs
@@ -7,7 +7,6 @@ enum _Enum {
 
 type _E = _Enum;
 
-// { dg-warning "function is never used: '_a'" "" { target *-*-* } .+1 }
 const fn _a() -> _Enum {
     _E::A()
 }
-- 
2.50.1



More information about the Gcc-rust mailing list