From 854b1331810b503e266be96708ed753db2677865 Mon Sep 17 00:00:00 2001 From: Arif Roktim Date: Wed, 21 Oct 2020 16:56:20 -0400 Subject: Properly identify camel cased acronyms as UpperCamelCase --- crates/hir_ty/src/diagnostics/decl_check.rs | 28 ++++++++++++++++++++-- .../hir_ty/src/diagnostics/decl_check/case_conv.rs | 9 ++++++- 2 files changed, 34 insertions(+), 3 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index f987636fe..f179c62b7 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs @@ -708,11 +708,23 @@ fn foo() { } #[test] - fn incorrect_struct_name() { + fn incorrect_struct_names() { check_diagnostics( r#" struct non_camel_case_name {} // ^^^^^^^^^^^^^^^^^^^ Structure `non_camel_case_name` should have CamelCase name, e.g. `NonCamelCaseName` + +struct SCREAMING_CASE {} + // ^^^^^^^^^^^^^^ Structure `SCREAMING_CASE` should have CamelCase name, e.g. `ScreamingCase` +"#, + ); + } + + #[test] + fn no_diagnostic_for_camel_cased_acronyms_in_struct_name() { + check_diagnostics( + r#" +struct AABB {} "#, ); } @@ -728,11 +740,23 @@ struct SomeStruct { SomeField: u8 } } #[test] - fn incorrect_enum_name() { + fn incorrect_enum_names() { check_diagnostics( r#" enum some_enum { Val(u8) } // ^^^^^^^^^ Enum `some_enum` should have CamelCase name, e.g. `SomeEnum` + +enum SOME_ENUM + // ^^^^^^^^^ Enum `SOME_ENUM` should have CamelCase name, e.g. `SomeEnum` +"#, + ); + } + + #[test] + fn no_diagnostic_for_camel_cased_acronyms_in_enum_name() { + check_diagnostics( + r#" +enum AABB {} "#, ); } diff --git a/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs b/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs index 3800f2a6b..324d60765 100644 --- a/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs +++ b/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs @@ -29,7 +29,13 @@ fn detect_case(ident: &str) -> DetectedCase { if has_uppercase { if !has_lowercase { - DetectedCase::UpperSnakeCase + if has_underscore { + DetectedCase::UpperSnakeCase + } else { + // It has uppercase only and no underscores. Ex: "AABB" + // This is a camel cased acronym. + DetectedCase::UpperCamelCase + } } else if !has_underscore { if first_lowercase { DetectedCase::LowerCamelCase @@ -180,6 +186,7 @@ mod tests { check(to_camel_case, "Weird_Case", expect![["WeirdCase"]]); check(to_camel_case, "name", expect![["Name"]]); check(to_camel_case, "A", expect![[""]]); + check(to_camel_case, "AABB", expect![[""]]); } #[test] -- cgit v1.2.3