Skip to content

Commit 5fe3b75

Browse files
committed
incorrect case diagnostics for trait name
1 parent 8f6a728 commit 5fe3b75

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use hir_def::{
2020
hir::{Pat, PatId},
2121
src::HasSource,
2222
AdtId, AttrDefId, ConstId, EnumId, FunctionId, ItemContainerId, Lookup, ModuleDefId, ModuleId,
23-
StaticId, StructId,
23+
StaticId, StructId, TraitId,
2424
};
2525
use hir_expand::{
2626
name::{AsName, Name},
@@ -79,12 +79,13 @@ pub enum IdentType {
7979
Enum,
8080
Field,
8181
Function,
82+
Module,
8283
Parameter,
8384
StaticVariable,
8485
Structure,
86+
Trait,
8587
Variable,
8688
Variant,
87-
Module,
8889
}
8990

9091
impl fmt::Display for IdentType {
@@ -94,12 +95,13 @@ impl fmt::Display for IdentType {
9495
IdentType::Enum => "Enum",
9596
IdentType::Field => "Field",
9697
IdentType::Function => "Function",
98+
IdentType::Module => "Module",
9799
IdentType::Parameter => "Parameter",
98100
IdentType::StaticVariable => "Static variable",
99101
IdentType::Structure => "Structure",
102+
IdentType::Trait => "Trait",
100103
IdentType::Variable => "Variable",
101104
IdentType::Variant => "Variant",
102-
IdentType::Module => "Module",
103105
};
104106

105107
repr.fmt(f)
@@ -136,6 +138,7 @@ impl<'a> DeclValidator<'a> {
136138
pub(super) fn validate_item(&mut self, item: ModuleDefId) {
137139
match item {
138140
ModuleDefId::ModuleId(module_id) => self.validate_module(module_id),
141+
ModuleDefId::TraitId(trait_id) => self.validate_trait(trait_id),
139142
ModuleDefId::FunctionId(func) => self.validate_func(func),
140143
ModuleDefId::AdtId(adt) => self.validate_adt(adt),
141144
ModuleDefId::ConstId(const_id) => self.validate_const(const_id),
@@ -283,6 +286,47 @@ impl<'a> DeclValidator<'a> {
283286
}
284287
}
285288

289+
fn validate_trait(&mut self, trait_id: TraitId) {
290+
// Check whether non-snake case identifiers are allowed for this trait.
291+
if self.allowed(trait_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
292+
return;
293+
}
294+
295+
// Check the trait name.
296+
let data = self.db.trait_data(trait_id);
297+
let trait_name = data.name.display(self.db.upcast()).to_string();
298+
let trait_name_replacement = to_camel_case(&trait_name).map(|new_name| Replacement {
299+
current_name: data.name.clone(),
300+
suggested_text: new_name,
301+
expected_case: CaseType::UpperCamelCase,
302+
});
303+
304+
if let Some(replacement) = trait_name_replacement {
305+
let trait_loc = trait_id.lookup(self.db.upcast());
306+
let trait_src = trait_loc.source(self.db.upcast());
307+
308+
let Some(ast_ptr) = trait_src.value.name() else {
309+
never!(
310+
"Replacement ({:?}) was generated for a trait without a name: {:?}",
311+
replacement,
312+
trait_src
313+
);
314+
return;
315+
};
316+
317+
let diagnostic = IncorrectCase {
318+
file: trait_src.file_id,
319+
ident_type: IdentType::Trait,
320+
ident: AstPtr::new(&ast_ptr),
321+
expected_case: replacement.expected_case,
322+
ident_text: trait_name,
323+
suggested_text: replacement.suggested_text,
324+
};
325+
326+
self.sink.push(diagnostic);
327+
}
328+
}
329+
286330
fn validate_func(&mut self, func: FunctionId) {
287331
let data = self.db.function_data(func);
288332
if matches!(func.lookup(self.db.upcast()).container, ItemContainerId::ExternBlockId(_)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,13 @@ extern {
462462
}
463463

464464
#[test]
465-
fn bug_traits_arent_checked() {
465+
fn incorrect_trait_and_assoc_item_names() {
466466
// FIXME: Traits and functions in traits aren't currently checked by
467467
// r-a, even though rustc will complain about them.
468468
check_diagnostics(
469469
r#"
470470
trait BAD_TRAIT {
471+
// ^^^^^^^^^ 💡 warn: Trait `BAD_TRAIT` should have CamelCase name, e.g. `BadTrait`
471472
fn BAD_FUNCTION();
472473
fn BadFunction();
473474
}

0 commit comments

Comments
 (0)