Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion gcc/rust/checks/lints/unused/rust-unused-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "rust-unused-checker.h"
#include "rust-hir-expr.h"
#include "rust-hir-item.h"

#include "options.h"
#include "rust-keyword-values.h"
#include <cctype>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a system header, which should not be used. Use rust-system.h instead.


namespace Rust {
namespace Analysis {
Expand Down Expand Up @@ -124,6 +124,55 @@ UnusedChecker::visit (HIR::EmptyStmt &stmt)
"unnecessary trailing semicolons");
}

bool
is_camel_case (Identifier identifier)
{
auto s = identifier.as_string ();
return ISUPPER (s.front ())
&& std::all_of (s.begin (), s.end (),
[] (unsigned char c) { return ISALNUM (c); });
}

void
UnusedChecker::visit (HIR::Trait &trait)
{
if (!is_camel_case (trait.get_name ()))
Comment on lines +137 to +139
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if having this as part of the UnusedChecker is great. I would rather see it as its own pass, or maybe as part of smaller, less-essential lints

rust_warning_at (trait.get_locus (), OPT_Wunused_variable,
"trait %qs should have an upper camel case name",
trait.get_name ().as_string ().c_str ());
walk (trait);
}

void
UnusedChecker::visit (HIR::StructStruct &strct)
{
if (!is_camel_case (strct.get_identifier ()))
rust_warning_at (strct.get_locus (), OPT_Wunused_variable,
"struct %qs should have an upper camel case name",
strct.get_identifier ().as_string ().c_str ());
walk (strct);
}

void
UnusedChecker::visit (HIR::TupleStruct &strct)
{
if (!is_camel_case (strct.get_identifier ()))
rust_warning_at (strct.get_locus (), OPT_Wunused_variable,
"struct %qs should have an upper camel case name",
strct.get_identifier ().as_string ().c_str ());
walk (strct);
}

void
UnusedChecker::visit (HIR::Enum &enm)
{
if (!is_camel_case (enm.get_identifier ()))
rust_warning_at (enm.get_locus (), OPT_Wunused_variable,
"enum %qs should have an upper camel case name",
enm.get_identifier ().as_string ().c_str ());
walk (enm);
}

void
UnusedChecker::visit_loop_label (HIR::LoopLabel &label)
{
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/checks/lints/unused/rust-unused-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
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::Trait &item) override;
virtual void visit (HIR::StructStruct &strct) override;
virtual void visit (HIR::TupleStruct &strct) override;
virtual void visit (HIR::Enum &enm) override;
virtual void visit_loop_label (HIR::LoopLabel &label) override;
};
} // namespace Analysis
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/non-camel-case-types_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// { dg-additional-options "-frust-unused-check-2.0" }

struct my_struct; // { dg-warning "struct is never constructed: .my.struct." }
// { dg-warning "struct .my.struct. should have an upper camel case name" "" { target *-*-* } .-1 }

enum my_enum {}
// { dg-warning "enum .my.enum. should have an upper camel case name" "" { target *-*-* } .-1 }

trait my_trait {}
// { dg-warning "trait .my.trait. should have an upper camel case name" "" { target *-*-* } .-1 }