From: Lucas Ly Ba <[email protected]>

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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/92888487add96854d5ed0033c455d252b5dba991

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4381

 .../lints/unused/rust-unused-checker.cc       | 45 +++++++++++++++++++
 .../checks/lints/unused/rust-unused-checker.h |  4 ++
 .../rust/compile/non-snake-case_0.rs          | 16 +++++++
 3 files changed, 65 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/non-snake-case_0.rs

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc 
b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index f8428c860..6ceda5515 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 eeb94679d..70ea660e7 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 000000000..bc1129a7a
--- /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 }
+

base-commit: a8a1fa3bb691f9e362529bba8f39f62e4dcff0df
-- 
2.52.0

Reply via email to