Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 33b7b45

Browse files
committed
Report incorrect case for inner items within all bodies
1 parent b53a078 commit 33b7b45

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

crates/hir-ty/src/diagnostics/decl_check.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use hir_def::{
1818
data::adt::VariantData,
1919
hir::{Pat, PatId},
2020
src::HasSource,
21-
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, ItemContainerId, Lookup,
22-
ModuleDefId, StaticId, StructId,
21+
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ItemContainerId,
22+
Lookup, ModuleDefId, StaticId, StructId,
2323
};
2424
use hir_expand::{
2525
name::{AsName, Name},
@@ -189,8 +189,7 @@ impl<'a> DeclValidator<'a> {
189189
AttrDefId::TypeAliasId(_) => None,
190190
AttrDefId::GenericParamId(_) => None,
191191
}
192-
.map(|mid| self.allowed(mid, allow_name, true))
193-
.unwrap_or(false)
192+
.is_some_and(|mid| self.allowed(mid, allow_name, true))
194193
}
195194

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

484+
for (local_id, _) in data.variants.iter() {
485+
let variant_id = EnumVariantId { parent: enum_id, local_id };
486+
self.validate_body_inner_items(variant_id.into());
487+
}
488+
485489
// Check whether non-camel case names are allowed for this enum.
486490
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
487491
return;
@@ -498,13 +502,11 @@ impl<'a> DeclValidator<'a> {
498502
// Check the field names.
499503
let enum_fields_replacements = data
500504
.variants
501-
.iter()
502-
.filter_map(|(_, variant)| {
505+
.values()
506+
.filter_map(|variant| {
503507
Some(Replacement {
504508
current_name: variant.name.clone(),
505-
suggested_text: to_camel_case(
506-
&variant.name.display(self.db.upcast()).to_string(),
507-
)?,
509+
suggested_text: to_camel_case(&variant.name.to_smol_str())?,
508510
expected_case: CaseType::UpperCamelCase,
509511
})
510512
})
@@ -608,6 +610,8 @@ impl<'a> DeclValidator<'a> {
608610
fn validate_const(&mut self, const_id: ConstId) {
609611
let data = self.db.const_data(const_id);
610612

613+
self.validate_body_inner_items(const_id.into());
614+
611615
if self.allowed(const_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
612616
return;
613617
}
@@ -617,7 +621,7 @@ impl<'a> DeclValidator<'a> {
617621
None => return,
618622
};
619623

620-
let const_name = name.display(self.db.upcast()).to_string();
624+
let const_name = name.to_smol_str();
621625
let replacement = if let Some(new_name) = to_upper_snake_case(&const_name) {
622626
Replacement {
623627
current_name: name.clone(),
@@ -656,13 +660,15 @@ impl<'a> DeclValidator<'a> {
656660
return;
657661
}
658662

663+
self.validate_body_inner_items(static_id.into());
664+
659665
if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
660666
return;
661667
}
662668

663669
let name = &data.name;
664670

665-
let static_name = name.display(self.db.upcast()).to_string();
671+
let static_name = name.to_smol_str();
666672
let replacement = if let Some(new_name) = to_upper_snake_case(&static_name) {
667673
Replacement {
668674
current_name: name.clone(),
@@ -694,6 +700,7 @@ impl<'a> DeclValidator<'a> {
694700
self.sink.push(diagnostic);
695701
}
696702

703+
// FIXME: We don't currently validate names within `DefWithBodyId::InTypeConstId`.
697704
/// Recursively validates inner scope items, such as static variables and constants.
698705
fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
699706
let body = self.db.body(body_id);

crates/ide-diagnostics/src/handlers/incorrect_case.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,73 @@ fn main() {
571571
//^^^^^^^^^^^ 💡 warn: Variable `INNER_LOCAL` should have snake_case name, e.g. `inner_local`
572572
}
573573
}
574+
"#,
575+
);
576+
}
577+
578+
#[test]
579+
fn const_body_inner_items() {
580+
check_diagnostics(
581+
r#"
582+
const _: () = {
583+
static bar: bool = true;
584+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
585+
fn BAZ() {}
586+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
587+
588+
const foo: () = {
589+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
590+
const foo: bool = true;
591+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
592+
static bar: bool = true;
593+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
594+
fn BAZ() {}
595+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
596+
};
597+
};
598+
"#,
599+
);
600+
}
601+
602+
#[test]
603+
fn static_body_inner_items() {
604+
check_diagnostics(
605+
r#"
606+
static FOO: () = {
607+
const foo: bool = true;
608+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
609+
fn BAZ() {}
610+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
611+
612+
static bar: () = {
613+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
614+
const foo: bool = true;
615+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
616+
static bar: bool = true;
617+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
618+
fn BAZ() {}
619+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
620+
};
621+
};
622+
"#,
623+
);
624+
}
625+
626+
#[test]
627+
fn enum_variant_body_inner_item() {
628+
check_diagnostics(
629+
r#"
630+
enum E {
631+
A = {
632+
const foo: bool = true;
633+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
634+
static bar: bool = true;
635+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
636+
fn BAZ() {}
637+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
638+
42
639+
},
640+
}
574641
"#,
575642
);
576643
}

0 commit comments

Comments
 (0)