Repository: rust-analyzer/rust-analyzer
Pull request #10820 (2021-11-20 16:26:36)

minor: Move incorrect case diagnostic things into their module
bors r+


Patch:
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs
index 38bbf41797..e9ace855fc 100644
--- a/crates/hir/src/display.rs
+++ b/crates/hir/src/display.rs
@@ -325,21 +325,7 @@ fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), H
 
 fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
     let params = f.db.generic_params(def);
-
-    // unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`.
-    let is_unnamed_type_target = |target: &WherePredicateTypeTarget| match target {
-        WherePredicateTypeTarget::TypeRef(_) => false,
-        WherePredicateTypeTarget::TypeParam(id) => params.types[*id].name.is_none(),
-    };
-
-    let has_displayable_predicate = params
-        .where_predicates
-        .iter()
-        .any(|pred| {
-            !matches!(pred, WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target))
-        });
-
-    if !has_displayable_predicate {
+    if params.where_predicates.is_empty() {
         return Ok(());
     }
 
@@ -362,7 +348,6 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
         };
 
         match pred {
-            WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target) => {}
             WherePredicate::TypeBound { target, bound } => {
                 if matches!(prev_pred, Some(WherePredicate::TypeBound { target: target_, .. }) if target_ == target)
                 {
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 964c791e48..3946f51642 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -359,7 +359,7 @@ impl ModuleDef {
                 def.diagnostics(db, &mut acc);
             }
             None => {
-                for diag in hir_ty::diagnostics::validate_module_item(db, module.id.krate(), id) {
+                for diag in hir_ty::diagnostics::incorrect_case(db, module.id.krate(), id) {
                     acc.push(diag.into())
                 }
             }
@@ -1282,7 +1282,7 @@ impl DefWithBody {
             DefWithBody::Static(it) => it.into(),
             DefWithBody::Const(it) => it.into(),
         };
-        for diag in hir_ty::diagnostics::validate_module_item(db, krate, def.into()) {
+        for diag in hir_ty::diagnostics::incorrect_case(db, krate, def.into()) {
             acc.push(diag.into())
         }
     }
diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs
index c84cd50ce6..3abc48d95d 100644
--- a/crates/hir_def/src/path/lower.rs
+++ b/crates/hir_def/src/path/lower.rs
@@ -76,7 +76,7 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
                         kind = mod_path.kind;
 
                         segments.extend(mod_path.segments.iter().cloned().rev());
-                        generic_args.extend(path_generic_args.iter().cloned().rev());
+                        generic_args.extend(Vec::from(path_generic_args).into_iter().rev());
 
                         // Insert the type reference (T in the above example) as Self parameter for the trait
                         let last_segment =
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs
index 6339c9687c..a4702715e5 100644
--- a/crates/hir_ty/src/diagnostics.rs
+++ b/crates/hir_ty/src/diagnostics.rs
@@ -4,92 +4,10 @@ mod match_check;
 mod unsafe_check;
 mod decl_check;
 
-use std::fmt;
-
-use base_db::CrateId;
-use hir_def::ModuleDefId;
-use hir_expand::HirFileId;
-use syntax::{ast, AstPtr};
-
-use crate::db::HirDatabase;
-
 pub use crate::diagnostics::{
+    decl_check::{incorrect_case, IncorrectCase},
     expr::{
         record_literal_missing_fields, record_pattern_missing_fields, BodyValidationDiagnostic,
     },
     unsafe_check::missing_unsafe,
 };
-
-pub fn validate_module_item(
-    db: &dyn HirDatabase,
-    krate: CrateId,
-    owner: ModuleDefId,
-) -> Vec<IncorrectCase> {
-    let _p = profile::span("validate_module_item");
-    let mut validator = decl_check::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,
-}
diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs
index 4f4a92447f..6a3588dc74 100644
--- a/crates/hir_ty/src/diagnostics/decl_check.rs
+++ b/crates/hir_ty/src/diagnostics/decl_check.rs
@@ -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,
@@ -19,17 +21,19 @@ use hir_def::{
     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, 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,


Test patch:
diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs
index c745f86d84..5db6fd7974 100644
--- a/crates/ide/src/hover/tests.rs
+++ b/crates/ide/src/hover/tests.rs
@@ -340,48 +340,6 @@ fn main() { m::f$0oo(); }
     );
 }
 
-#[test]
-fn hover_omits_unnamed_where_preds() {
-    check(
-        r#"
-pub fn foo(bar: impl T) { }
-
-fn main() { fo$0o(); }
-        "#,
-        expect![[r#"
-            *foo*
-
-            ```rust
-            test
-            ```
-
-            ```rust
-            pub fn foo(bar: impl T)
-            ```
-        "#]],
-    );
-    check(
-        r#"
-pub fn foo<V: AsRef<str>>(bar: impl T, baz: V) { }
-
-fn main() { fo$0o(); }
-        "#,
-        expect![[r#"
-            *foo*
-
-            ```rust
-            test
-            ```
-
-            ```rust
-            pub fn foo<V>(bar: impl T, baz: V)
-            where
-                V: AsRef<str>,
-            ```
-        "#]],
-    );
-}
-
 #[test]
 fn hover_shows_fn_signature_with_type_params() {
     check(