Skip to content

Commit 9288848

Browse files
lucasly-baP-E-P
authored andcommitted
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]>
1 parent a8a1fa3 commit 9288848

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

gcc/rust/checks/lints/unused/rust-unused-checker.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "rust-unused-checker.h"
2020
#include "rust-hir-expr.h"
21+
#include "rust-hir-generic-param.h"
2122
#include "rust-hir-item.h"
2223

2324
#include "options.h"
@@ -39,6 +40,15 @@ UnusedChecker::go (HIR::Crate &crate)
3940
item->accept_vis (*this);
4041
}
4142

43+
bool
44+
is_snake_case (Identifier identifier)
45+
{
46+
auto s = identifier.as_string ();
47+
return std::all_of (s.begin (), s.end (), [] (unsigned char c) {
48+
return ISLOWER (c) || ISDIGIT (c) || c == '_';
49+
});
50+
}
51+
4252
void
4353
UnusedChecker::visit (HIR::ConstantItem &item)
4454
{
@@ -89,6 +99,11 @@ UnusedChecker::visit (HIR::IdentifierPattern &pattern)
8999
rust_warning_at (pattern.get_locus (), OPT_Wunused_variable,
90100
"unused mut %qs",
91101
pattern.get_identifier ().as_string ().c_str ());
102+
103+
if (!is_snake_case (pattern.get_identifier ()))
104+
rust_warning_at (pattern.get_locus (), OPT_Wunused_variable,
105+
"variable %qs should have a snake case name",
106+
var_name.c_str ());
92107
}
93108
void
94109

@@ -131,6 +146,36 @@ UnusedChecker::visit (HIR::EmptyStmt &stmt)
131146
"unnecessary trailing semicolons");
132147
}
133148

149+
void
150+
UnusedChecker::visit (HIR::Function &fct)
151+
{
152+
if (!is_snake_case (fct.get_function_name ()))
153+
rust_warning_at (fct.get_locus (), OPT_Wunused_variable,
154+
"function %qs should have a snake case name",
155+
fct.get_function_name ().as_string ().c_str ());
156+
walk (fct);
157+
}
158+
159+
void
160+
UnusedChecker::visit (HIR::Module &mod)
161+
{
162+
if (!is_snake_case (mod.get_module_name ()))
163+
rust_warning_at (mod.get_locus (), OPT_Wunused_variable,
164+
"module %qs should have a snake case name",
165+
mod.get_module_name ().as_string ().c_str ());
166+
walk (mod);
167+
}
168+
169+
void
170+
UnusedChecker::visit (HIR::LifetimeParam &lft)
171+
{
172+
if (!is_snake_case (lft.get_lifetime ().get_name ()))
173+
rust_warning_at (lft.get_locus (), OPT_Wunused_variable,
174+
"lifetime %qs should have a snake case name",
175+
lft.get_lifetime ().get_name ().c_str ());
176+
walk (lft);
177+
}
178+
134179
void
135180
UnusedChecker::visit_loop_label (HIR::LoopLabel &label)
136181
{

gcc/rust/checks/lints/unused/rust-unused-checker.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-hir-expr.h"
20+
#include "rust-hir-generic-param.h"
2021
#include "rust-hir-item.h"
2122
#include "rust-hir-pattern.h"
2223
#include "rust-hir-visitor.h"
@@ -44,6 +45,9 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
4445
virtual void visit (HIR::AssignmentExpr &identifier) override;
4546
virtual void visit (HIR::StructPatternFieldIdent &identifier) override;
4647
virtual void visit (HIR::EmptyStmt &stmt) override;
48+
virtual void visit (HIR::Function &fct) override;
49+
virtual void visit (HIR::Module &mod) override;
50+
virtual void visit (HIR::LifetimeParam &lft) override;
4751
virtual void visit_loop_label (HIR::LoopLabel &label) override;
4852
};
4953
} // namespace Analysis
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// { dg-additional-options "-frust-unused-check-2.0" }
2+
3+
pub fn MyFunction() {
4+
// { dg-warning "function .MyFunction. should have a snake case name" "" { target *-*-* } .-1 }
5+
let _MyVar = 2;
6+
// { dg-warning "variable ._MyVar. should have a snake case name" "" { target *-*-* } .-1 }
7+
}
8+
9+
pub fn foo<'MyLifetime>(x: &'MyLifetime i32) -> &'MyLifetime i32 {
10+
// { dg-warning "lifetime .MyLifetime. should have a snake case name" "" { target *-*-* } .-1 }
11+
x
12+
}
13+
14+
mod MyModule {}
15+
// { dg-warning "module .MyModule. should have a snake case name" "" { target *-*-* } .-1 }
16+

0 commit comments

Comments
 (0)