Skip to content

Commit

Permalink
Report incorrect case for inner items within all bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
lowr committed Jul 21, 2023
1 parent b53a078 commit 33b7b45
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
29 changes: 18 additions & 11 deletions crates/hir-ty/src/diagnostics/decl_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use hir_def::{
data::adt::VariantData,
hir::{Pat, PatId},
src::HasSource,
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, ItemContainerId, Lookup,
ModuleDefId, StaticId, StructId,
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ItemContainerId,
Lookup, ModuleDefId, StaticId, StructId,
};
use hir_expand::{
name::{AsName, Name},
Expand Down Expand Up @@ -189,8 +189,7 @@ impl<'a> DeclValidator<'a> {
AttrDefId::TypeAliasId(_) => None,
AttrDefId::GenericParamId(_) => None,
}
.map(|mid| self.allowed(mid, allow_name, true))
.unwrap_or(false)
.is_some_and(|mid| self.allowed(mid, allow_name, true))
}

fn validate_func(&mut self, func: FunctionId) {
Expand Down Expand Up @@ -482,6 +481,11 @@ impl<'a> DeclValidator<'a> {
fn validate_enum(&mut self, enum_id: EnumId) {
let data = self.db.enum_data(enum_id);

for (local_id, _) in data.variants.iter() {
let variant_id = EnumVariantId { parent: enum_id, local_id };
self.validate_body_inner_items(variant_id.into());
}

// Check whether non-camel case names are allowed for this enum.
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
return;
Expand All @@ -498,13 +502,11 @@ impl<'a> DeclValidator<'a> {
// Check the field names.
let enum_fields_replacements = data
.variants
.iter()
.filter_map(|(_, variant)| {
.values()
.filter_map(|variant| {
Some(Replacement {
current_name: variant.name.clone(),
suggested_text: to_camel_case(
&variant.name.display(self.db.upcast()).to_string(),
)?,
suggested_text: to_camel_case(&variant.name.to_smol_str())?,
expected_case: CaseType::UpperCamelCase,
})
})
Expand Down Expand Up @@ -608,6 +610,8 @@ impl<'a> DeclValidator<'a> {
fn validate_const(&mut self, const_id: ConstId) {
let data = self.db.const_data(const_id);

self.validate_body_inner_items(const_id.into());

if self.allowed(const_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
return;
}
Expand All @@ -617,7 +621,7 @@ impl<'a> DeclValidator<'a> {
None => return,
};

let const_name = name.display(self.db.upcast()).to_string();
let const_name = name.to_smol_str();
let replacement = if let Some(new_name) = to_upper_snake_case(&const_name) {
Replacement {
current_name: name.clone(),
Expand Down Expand Up @@ -656,13 +660,15 @@ impl<'a> DeclValidator<'a> {
return;
}

self.validate_body_inner_items(static_id.into());

if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
return;
}

let name = &data.name;

let static_name = name.display(self.db.upcast()).to_string();
let static_name = name.to_smol_str();
let replacement = if let Some(new_name) = to_upper_snake_case(&static_name) {
Replacement {
current_name: name.clone(),
Expand Down Expand Up @@ -694,6 +700,7 @@ impl<'a> DeclValidator<'a> {
self.sink.push(diagnostic);
}

// FIXME: We don't currently validate names within `DefWithBodyId::InTypeConstId`.
/// Recursively validates inner scope items, such as static variables and constants.
fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
let body = self.db.body(body_id);
Expand Down
67 changes: 67 additions & 0 deletions crates/ide-diagnostics/src/handlers/incorrect_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,73 @@ fn main() {
//^^^^^^^^^^^ πŸ’‘ warn: Variable `INNER_LOCAL` should have snake_case name, e.g. `inner_local`
}
}
"#,
);
}

#[test]
fn const_body_inner_items() {
check_diagnostics(
r#"
const _: () = {
static bar: bool = true;
//^^^ πŸ’‘ warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
fn BAZ() {}
//^^^ πŸ’‘ warn: Function `BAZ` should have snake_case name, e.g. `baz`
const foo: () = {
//^^^ πŸ’‘ warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
const foo: bool = true;
//^^^ πŸ’‘ warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
static bar: bool = true;
//^^^ πŸ’‘ warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
fn BAZ() {}
//^^^ πŸ’‘ warn: Function `BAZ` should have snake_case name, e.g. `baz`
};
};
"#,
);
}

#[test]
fn static_body_inner_items() {
check_diagnostics(
r#"
static FOO: () = {
const foo: bool = true;
//^^^ πŸ’‘ warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
fn BAZ() {}
//^^^ πŸ’‘ warn: Function `BAZ` should have snake_case name, e.g. `baz`
static bar: () = {
//^^^ πŸ’‘ warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
const foo: bool = true;
//^^^ πŸ’‘ warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
static bar: bool = true;
//^^^ πŸ’‘ warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
fn BAZ() {}
//^^^ πŸ’‘ warn: Function `BAZ` should have snake_case name, e.g. `baz`
};
};
"#,
);
}

#[test]
fn enum_variant_body_inner_item() {
check_diagnostics(
r#"
enum E {
A = {
const foo: bool = true;
//^^^ πŸ’‘ warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
static bar: bool = true;
//^^^ πŸ’‘ warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
fn BAZ() {}
//^^^ πŸ’‘ warn: Function `BAZ` should have snake_case name, e.g. `baz`
42
},
}
"#,
);
}
Expand Down

0 comments on commit 33b7b45

Please sign in to comment.