Report incorrect case for inner items within all bodies
This commit is contained in:
parent
b53a07835b
commit
33b7b45f67
@ -18,8 +18,8 @@
|
||||
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},
|
||||
@ -189,8 +189,7 @@ fn allowed(&self, id: AttrDefId, allow_name: &str, recursing: bool) -> bool {
|
||||
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) {
|
||||
@ -482,6 +481,11 @@ fn create_incorrect_case_diagnostic_for_struct(
|
||||
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;
|
||||
@ -498,13 +502,11 @@ fn validate_enum(&mut self, enum_id: EnumId) {
|
||||
// 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,
|
||||
})
|
||||
})
|
||||
@ -608,6 +610,8 @@ fn create_incorrect_case_diagnostic_for_enum(
|
||||
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;
|
||||
}
|
||||
@ -617,7 +621,7 @@ fn validate_const(&mut self, const_id: ConstId) {
|
||||
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(),
|
||||
@ -656,13 +660,15 @@ fn validate_static(&mut self, static_id: StaticId) {
|
||||
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(),
|
||||
@ -694,6 +700,7 @@ fn validate_static(&mut self, static_id: StaticId) {
|
||||
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);
|
||||
|
@ -571,6 +571,73 @@ fn BAZ() {
|
||||
//^^^^^^^^^^^ 💡 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
|
||||
},
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user