]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_ty/src/diagnostics/decl_check.rs
Move incorrect case diagnostic things into their module
[rust.git] / crates / hir_ty / src / diagnostics / decl_check.rs
index f26150b77fab404e95b6095e1d6ad8767d5fa4fb..6a3588dc741a0cff736c4fc2ed2e2f93902f3518 100644 (file)
@@ -1,4 +1,4 @@
-//! Provides validators for the item declarations.
+//! Provides validators for names of declarations.
 //!
 //! This includes the following items:
 //!
@@ -12,6 +12,8 @@
 
 mod case_conv;
 
+use std::fmt;
+
 use base_db::CrateId;
 use hir_def::{
     adt::VariantData,
     src::HasSource,
     AdtId, AttrDefId, ConstId, EnumId, FunctionId, Lookup, ModuleDefId, StaticId, StructId,
 };
-use hir_expand::name::{AsName, Name};
+use hir_expand::{
+    name::{AsName, Name},
+    HirFileId,
+};
 use stdx::{always, never};
 use syntax::{
-    ast::{self, NameOwner},
+    ast::{self, HasName},
     AstNode, AstPtr,
 };
 
-use crate::{
-    db::HirDatabase,
-    diagnostics::{decl_check::case_conv::*, CaseType, IdentType, IncorrectCase},
-};
+use crate::db::HirDatabase;
+
+use self::case_conv::{to_camel_case, to_lower_snake_case, to_upper_snake_case};
 
 mod allow {
     pub(super) const BAD_STYLE: &str = "bad_style";
@@ -39,6 +43,80 @@ mod allow {
     pub(super) const NON_CAMEL_CASE_TYPES: &str = "non_camel_case_types";
 }
 
+pub fn incorrect_case(
+    db: &dyn HirDatabase,
+    krate: CrateId,
+    owner: ModuleDefId,
+) -> Vec<IncorrectCase> {
+    let _p = profile::span("validate_module_item");
+    let mut validator = DeclValidator::new(db, krate);
+    validator.validate_item(owner);
+    validator.sink
+}
+
+#[derive(Debug)]
+pub enum CaseType {
+    // `some_var`
+    LowerSnakeCase,
+    // `SOME_CONST`
+    UpperSnakeCase,
+    // `SomeStruct`
+    UpperCamelCase,
+}
+
+impl fmt::Display for CaseType {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let repr = match self {
+            CaseType::LowerSnakeCase => "snake_case",
+            CaseType::UpperSnakeCase => "UPPER_SNAKE_CASE",
+            CaseType::UpperCamelCase => "CamelCase",
+        };
+
+        write!(f, "{}", repr)
+    }
+}
+
+#[derive(Debug)]
+pub enum IdentType {
+    Constant,
+    Enum,
+    Field,
+    Function,
+    Parameter,
+    StaticVariable,
+    Structure,
+    Variable,
+    Variant,
+}
+
+impl fmt::Display for IdentType {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let repr = match self {
+            IdentType::Constant => "Constant",
+            IdentType::Enum => "Enum",
+            IdentType::Field => "Field",
+            IdentType::Function => "Function",
+            IdentType::Parameter => "Parameter",
+            IdentType::StaticVariable => "Static variable",
+            IdentType::Structure => "Structure",
+            IdentType::Variable => "Variable",
+            IdentType::Variant => "Variant",
+        };
+
+        write!(f, "{}", repr)
+    }
+}
+
+#[derive(Debug)]
+pub struct IncorrectCase {
+    pub file: HirFileId,
+    pub ident: AstPtr<ast::Name>,
+    pub expected_case: CaseType,
+    pub ident_type: IdentType,
+    pub ident_text: String,
+    pub suggested_text: String,
+}
+
 pub(super) struct DeclValidator<'a> {
     db: &'a dyn HirDatabase,
     krate: CrateId,
@@ -63,7 +141,7 @@ pub(super) fn validate_item(&mut self, item: ModuleDefId) {
             ModuleDefId::AdtId(adt) => self.validate_adt(adt),
             ModuleDefId::ConstId(const_id) => self.validate_const(const_id),
             ModuleDefId::StaticId(static_id) => self.validate_static(static_id),
-            _ => return,
+            _ => (),
         }
     }
 
@@ -581,10 +659,7 @@ fn validate_static(&mut self, static_id: StaticId) {
             return;
         }
 
-        let name = match &data.name {
-            Some(name) => name,
-            None => return,
-        };
+        let name = &data.name;
 
         let static_name = name.to_string();
         let replacement = if let Some(new_name) = to_upper_snake_case(&static_name) {