mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Report incorrect case for inner items within all bodies
This commit is contained in:
parent
b53a07835b
commit
33b7b45f67
2 changed files with 85 additions and 11 deletions
|
@ -18,8 +18,8 @@ use hir_def::{
|
||||||
data::adt::VariantData,
|
data::adt::VariantData,
|
||||||
hir::{Pat, PatId},
|
hir::{Pat, PatId},
|
||||||
src::HasSource,
|
src::HasSource,
|
||||||
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, ItemContainerId, Lookup,
|
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ItemContainerId,
|
||||||
ModuleDefId, StaticId, StructId,
|
Lookup, ModuleDefId, StaticId, StructId,
|
||||||
};
|
};
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
name::{AsName, Name},
|
name::{AsName, Name},
|
||||||
|
@ -189,8 +189,7 @@ impl<'a> DeclValidator<'a> {
|
||||||
AttrDefId::TypeAliasId(_) => None,
|
AttrDefId::TypeAliasId(_) => None,
|
||||||
AttrDefId::GenericParamId(_) => None,
|
AttrDefId::GenericParamId(_) => None,
|
||||||
}
|
}
|
||||||
.map(|mid| self.allowed(mid, allow_name, true))
|
.is_some_and(|mid| self.allowed(mid, allow_name, true))
|
||||||
.unwrap_or(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_func(&mut self, func: FunctionId) {
|
fn validate_func(&mut self, func: FunctionId) {
|
||||||
|
@ -482,6 +481,11 @@ impl<'a> DeclValidator<'a> {
|
||||||
fn validate_enum(&mut self, enum_id: EnumId) {
|
fn validate_enum(&mut self, enum_id: EnumId) {
|
||||||
let data = self.db.enum_data(enum_id);
|
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.
|
// Check whether non-camel case names are allowed for this enum.
|
||||||
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
|
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
|
||||||
return;
|
return;
|
||||||
|
@ -498,13 +502,11 @@ impl<'a> DeclValidator<'a> {
|
||||||
// Check the field names.
|
// Check the field names.
|
||||||
let enum_fields_replacements = data
|
let enum_fields_replacements = data
|
||||||
.variants
|
.variants
|
||||||
.iter()
|
.values()
|
||||||
.filter_map(|(_, variant)| {
|
.filter_map(|variant| {
|
||||||
Some(Replacement {
|
Some(Replacement {
|
||||||
current_name: variant.name.clone(),
|
current_name: variant.name.clone(),
|
||||||
suggested_text: to_camel_case(
|
suggested_text: to_camel_case(&variant.name.to_smol_str())?,
|
||||||
&variant.name.display(self.db.upcast()).to_string(),
|
|
||||||
)?,
|
|
||||||
expected_case: CaseType::UpperCamelCase,
|
expected_case: CaseType::UpperCamelCase,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -608,6 +610,8 @@ impl<'a> DeclValidator<'a> {
|
||||||
fn validate_const(&mut self, const_id: ConstId) {
|
fn validate_const(&mut self, const_id: ConstId) {
|
||||||
let data = self.db.const_data(const_id);
|
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) {
|
if self.allowed(const_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -617,7 +621,7 @@ impl<'a> DeclValidator<'a> {
|
||||||
None => return,
|
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) {
|
let replacement = if let Some(new_name) = to_upper_snake_case(&const_name) {
|
||||||
Replacement {
|
Replacement {
|
||||||
current_name: name.clone(),
|
current_name: name.clone(),
|
||||||
|
@ -656,13 +660,15 @@ impl<'a> DeclValidator<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.validate_body_inner_items(static_id.into());
|
||||||
|
|
||||||
if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
|
if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = &data.name;
|
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) {
|
let replacement = if let Some(new_name) = to_upper_snake_case(&static_name) {
|
||||||
Replacement {
|
Replacement {
|
||||||
current_name: name.clone(),
|
current_name: name.clone(),
|
||||||
|
@ -694,6 +700,7 @@ impl<'a> DeclValidator<'a> {
|
||||||
self.sink.push(diagnostic);
|
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.
|
/// Recursively validates inner scope items, such as static variables and constants.
|
||||||
fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
|
fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
|
||||||
let body = self.db.body(body_id);
|
let body = self.db.body(body_id);
|
||||||
|
|
|
@ -571,6 +571,73 @@ fn main() {
|
||||||
//^^^^^^^^^^^ 💡 warn: Variable `INNER_LOCAL` should have snake_case name, e.g. `inner_local`
|
//^^^^^^^^^^^ 💡 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…
Add table
Add a link
Reference in a new issue