https://gcc.gnu.org/g:c7d31bfc6331ab90c0fa1ac6f49a27d6b0afe67b
commit r16-7775-gc7d31bfc6331ab90c0fa1ac6f49a27d6b0afe67b Author: Lucas Ly Ba <[email protected]> Date: Wed Jan 14 16:08:43 2026 +0000 gccrs: add non snake case lint gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (is_snake_case): Add warning for variables, methods, functions, lifetime parameters and modules. (UnusedChecker::visit): New. * checks/lints/unused/rust-unused-checker.h: New. gcc/testsuite/ChangeLog: * rust/compile/non-snake-case_0.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]> Diff: --- .../checks/lints/unused/rust-unused-checker.cc | 45 ++++++++++++++++++++++ gcc/rust/checks/lints/unused/rust-unused-checker.h | 4 ++ gcc/testsuite/rust/compile/non-snake-case_0.rs | 16 ++++++++ 3 files changed, 65 insertions(+) diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc index f8428c8605be..6ceda5515cce 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc @@ -18,6 +18,7 @@ #include "rust-unused-checker.h" #include "rust-hir-expr.h" +#include "rust-hir-generic-param.h" #include "rust-hir-item.h" #include "options.h" @@ -39,6 +40,15 @@ UnusedChecker::go (HIR::Crate &crate) item->accept_vis (*this); } +bool +is_snake_case (Identifier identifier) +{ + auto s = identifier.as_string (); + return std::all_of (s.begin (), s.end (), [] (unsigned char c) { + return ISLOWER (c) || ISDIGIT (c) || c == '_'; + }); +} + void UnusedChecker::visit (HIR::ConstantItem &item) { @@ -89,6 +99,11 @@ UnusedChecker::visit (HIR::IdentifierPattern &pattern) rust_warning_at (pattern.get_locus (), OPT_Wunused_variable, "unused mut %qs", pattern.get_identifier ().as_string ().c_str ()); + + if (!is_snake_case (pattern.get_identifier ())) + rust_warning_at (pattern.get_locus (), OPT_Wunused_variable, + "variable %qs should have a snake case name", + var_name.c_str ()); } void @@ -131,6 +146,36 @@ UnusedChecker::visit (HIR::EmptyStmt &stmt) "unnecessary trailing semicolons"); } +void +UnusedChecker::visit (HIR::Function &fct) +{ + if (!is_snake_case (fct.get_function_name ())) + rust_warning_at (fct.get_locus (), OPT_Wunused_variable, + "function %qs should have a snake case name", + fct.get_function_name ().as_string ().c_str ()); + walk (fct); +} + +void +UnusedChecker::visit (HIR::Module &mod) +{ + if (!is_snake_case (mod.get_module_name ())) + rust_warning_at (mod.get_locus (), OPT_Wunused_variable, + "module %qs should have a snake case name", + mod.get_module_name ().as_string ().c_str ()); + walk (mod); +} + +void +UnusedChecker::visit (HIR::LifetimeParam &lft) +{ + if (!is_snake_case (lft.get_lifetime ().get_name ())) + rust_warning_at (lft.get_locus (), OPT_Wunused_variable, + "lifetime %qs should have a snake case name", + lft.get_lifetime ().get_name ().c_str ()); + walk (lft); +} + void UnusedChecker::visit_loop_label (HIR::LoopLabel &label) { diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.h b/gcc/rust/checks/lints/unused/rust-unused-checker.h index eeb94679ddd2..70ea660e7880 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.h +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.h @@ -17,6 +17,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-hir-expr.h" +#include "rust-hir-generic-param.h" #include "rust-hir-item.h" #include "rust-hir-pattern.h" #include "rust-hir-visitor.h" @@ -44,6 +45,9 @@ private: virtual void visit (HIR::AssignmentExpr &identifier) override; virtual void visit (HIR::StructPatternFieldIdent &identifier) override; virtual void visit (HIR::EmptyStmt &stmt) override; + virtual void visit (HIR::Function &fct) override; + virtual void visit (HIR::Module &mod) override; + virtual void visit (HIR::LifetimeParam &lft) override; virtual void visit_loop_label (HIR::LoopLabel &label) override; }; } // namespace Analysis diff --git a/gcc/testsuite/rust/compile/non-snake-case_0.rs b/gcc/testsuite/rust/compile/non-snake-case_0.rs new file mode 100644 index 000000000000..bc1129a7acd8 --- /dev/null +++ b/gcc/testsuite/rust/compile/non-snake-case_0.rs @@ -0,0 +1,16 @@ +// { dg-additional-options "-frust-unused-check-2.0" } + +pub fn MyFunction() { +// { dg-warning "function .MyFunction. should have a snake case name" "" { target *-*-* } .-1 } + let _MyVar = 2; +// { dg-warning "variable ._MyVar. should have a snake case name" "" { target *-*-* } .-1 } +} + +pub fn foo<'MyLifetime>(x: &'MyLifetime i32) -> &'MyLifetime i32 { +// { dg-warning "lifetime .MyLifetime. should have a snake case name" "" { target *-*-* } .-1 } + x +} + +mod MyModule {} +// { dg-warning "module .MyModule. should have a snake case name" "" { target *-*-* } .-1 } +
