Skip to content

Commit ea552aa

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 ea552aa

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 46 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,51 @@ 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+
}
144+
145+
void
146+
UnusedChecker::visit (HIR::StructStruct &strct)
147+
{
148+
if (!is_camel_case (strct.get_identifier ()))
149+
rust_warning_at (strct.get_locus (), OPT_Wunused_variable,
150+
"struct %qs should have an upper camel case name",
151+
strct.get_identifier ().as_string ().c_str ());
152+
}
153+
154+
void
155+
UnusedChecker::visit (HIR::TupleStruct &strct)
156+
{
157+
if (!is_camel_case (strct.get_identifier ()))
158+
rust_warning_at (strct.get_locus (), OPT_Wunused_variable,
159+
"struct %qs should have an upper camel case name",
160+
strct.get_identifier ().as_string ().c_str ());
161+
}
162+
163+
void
164+
UnusedChecker::visit (HIR::Enum &enm)
165+
{
166+
if (!is_camel_case (enm.get_identifier ()))
167+
rust_warning_at (enm.get_locus (), OPT_Wunused_variable,
168+
"enum %qs should have an upper camel case name",
169+
enm.get_identifier ().as_string ().c_str ());
170+
}
171+
127172
void
128173
UnusedChecker::visit_loop_label (HIR::LoopLabel &label)
129174
{

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)