Skip to content

Commit 1525d29

Browse files
committed
gccrs: add non camel case types lint
gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (is_camel_case): Add warning for struct, enum, and trait. (UnusedChecker::visit): New. * checks/lints/unused/rust-unused-checker.h: New. gcc/testsuite/ChangeLog: * rust/compile/non-camel-case-types_0.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
1 parent 17231c8 commit 1525d29

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "rust-unused-checker.h"
2020
#include "rust-hir-expr.h"
2121
#include "rust-hir-item.h"
22-
2322
#include "options.h"
2423
#include "rust-keyword-values.h"
24+
#include <cctype>
2525

2626
namespace Rust {
2727
namespace Analysis {
@@ -124,6 +124,55 @@ UnusedChecker::visit (HIR::EmptyStmt &stmt)
124124
"unnecessary trailing semicolons");
125125
}
126126

127+
bool
128+
is_camel_case (Identifier identifier)
129+
{
130+
auto s = identifier.as_string ();
131+
return ISUPPER (s.front ())
132+
&& std::all_of (s.begin (), s.end (),
133+
[] (unsigned char c) { return ISALNUM (c); });
134+
}
135+
136+
void
137+
UnusedChecker::visit (HIR::Trait &trait)
138+
{
139+
if (!is_camel_case (trait.get_name ()))
140+
rust_warning_at (trait.get_locus (), OPT_Wunused_variable,
141+
"trait %qs should have an upper camel case name",
142+
trait.get_name ().as_string ().c_str ());
143+
walk (trait);
144+
}
145+
146+
void
147+
UnusedChecker::visit (HIR::StructStruct &strct)
148+
{
149+
if (!is_camel_case (strct.get_identifier ()))
150+
rust_warning_at (strct.get_locus (), OPT_Wunused_variable,
151+
"struct %qs should have an upper camel case name",
152+
strct.get_identifier ().as_string ().c_str ());
153+
walk (strct);
154+
}
155+
156+
void
157+
UnusedChecker::visit (HIR::TupleStruct &strct)
158+
{
159+
if (!is_camel_case (strct.get_identifier ()))
160+
rust_warning_at (strct.get_locus (), OPT_Wunused_variable,
161+
"struct %qs should have an upper camel case name",
162+
strct.get_identifier ().as_string ().c_str ());
163+
walk (strct);
164+
}
165+
166+
void
167+
UnusedChecker::visit (HIR::Enum &enm)
168+
{
169+
if (!is_camel_case (enm.get_identifier ()))
170+
rust_warning_at (enm.get_locus (), OPT_Wunused_variable,
171+
"enum %qs should have an upper camel case name",
172+
enm.get_identifier ().as_string ().c_str ());
173+
walk (enm);
174+
}
175+
127176
void
128177
UnusedChecker::visit_loop_label (HIR::LoopLabel &label)
129178
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
4444
virtual void visit (HIR::AssignmentExpr &identifier) override;
4545
virtual void visit (HIR::StructPatternFieldIdent &identifier) override;
4646
virtual void visit (HIR::EmptyStmt &stmt) override;
47+
virtual void visit (HIR::Trait &item) override;
48+
virtual void visit (HIR::StructStruct &strct) override;
49+
virtual void visit (HIR::TupleStruct &strct) override;
50+
virtual void visit (HIR::Enum &enm) override;
4751
virtual void visit_loop_label (HIR::LoopLabel &label) override;
4852
};
4953
} // namespace Analysis
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// { dg-additional-options "-frust-unused-check-2.0" }
2+
3+
struct my_struct; // { dg-warning "struct is never constructed: .my.struct." }
4+
// { dg-warning "struct .my.struct. should have an upper camel case name" "" { target *-*-* } .-1 }
5+
6+
enum my_enum {}
7+
// { dg-warning "enum .my.enum. should have an upper camel case name" "" { target *-*-* } .-1 }
8+
9+
trait my_trait {}
10+
// { dg-warning "trait .my.trait. should have an upper camel case name" "" { target *-*-* } .-1 }
11+

0 commit comments

Comments
 (0)