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

Commit b53a078

Browse files
committed
Report incorrect case for fn inner items
1 parent cc2f0ec commit b53a078

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ mod case_conv;
1414

1515
use std::fmt;
1616

17-
use base_db::CrateId;
1817
use hir_def::{
1918
data::adt::VariantData,
2019
hir::{Pat, PatId},
2120
src::HasSource,
22-
AdtId, AttrDefId, ConstId, EnumId, FunctionId, ItemContainerId, Lookup, ModuleDefId, StaticId,
23-
StructId,
21+
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, ItemContainerId, Lookup,
22+
ModuleDefId, StaticId, StructId,
2423
};
2524
use hir_expand::{
2625
name::{AsName, Name},
@@ -44,13 +43,9 @@ mod allow {
4443
pub(super) const NON_CAMEL_CASE_TYPES: &str = "non_camel_case_types";
4544
}
4645

47-
pub fn incorrect_case(
48-
db: &dyn HirDatabase,
49-
krate: CrateId,
50-
owner: ModuleDefId,
51-
) -> Vec<IncorrectCase> {
46+
pub fn incorrect_case(db: &dyn HirDatabase, owner: ModuleDefId) -> Vec<IncorrectCase> {
5247
let _p = profile::span("validate_module_item");
53-
let mut validator = DeclValidator::new(db, krate);
48+
let mut validator = DeclValidator::new(db);
5449
validator.validate_item(owner);
5550
validator.sink
5651
}
@@ -120,7 +115,6 @@ pub struct IncorrectCase {
120115

121116
pub(super) struct DeclValidator<'a> {
122117
db: &'a dyn HirDatabase,
123-
krate: CrateId,
124118
pub(super) sink: Vec<IncorrectCase>,
125119
}
126120

@@ -132,8 +126,8 @@ struct Replacement {
132126
}
133127

134128
impl<'a> DeclValidator<'a> {
135-
pub(super) fn new(db: &'a dyn HirDatabase, krate: CrateId) -> DeclValidator<'a> {
136-
DeclValidator { db, krate, sink: Vec::new() }
129+
pub(super) fn new(db: &'a dyn HirDatabase) -> DeclValidator<'a> {
130+
DeclValidator { db, sink: Vec::new() }
137131
}
138132

139133
pub(super) fn validate_item(&mut self, item: ModuleDefId) {
@@ -206,17 +200,7 @@ impl<'a> DeclValidator<'a> {
206200
return;
207201
}
208202

209-
let body = self.db.body(func.into());
210-
211-
// Recursively validate inner scope items, such as static variables and constants.
212-
for (_, block_def_map) in body.blocks(self.db.upcast()) {
213-
for (_, module) in block_def_map.modules() {
214-
for def_id in module.scope.declarations() {
215-
let mut validator = DeclValidator::new(self.db, self.krate);
216-
validator.validate_item(def_id);
217-
}
218-
}
219-
}
203+
self.validate_body_inner_items(func.into());
220204

221205
// Check whether non-snake case identifiers are allowed for this function.
222206
if self.allowed(func.into(), allow::NON_SNAKE_CASE, false) {
@@ -231,6 +215,8 @@ impl<'a> DeclValidator<'a> {
231215
expected_case: CaseType::LowerSnakeCase,
232216
});
233217

218+
let body = self.db.body(func.into());
219+
234220
// Check the patterns inside the function body.
235221
// This includes function parameters.
236222
let pats_replacements = body
@@ -707,4 +693,16 @@ impl<'a> DeclValidator<'a> {
707693

708694
self.sink.push(diagnostic);
709695
}
696+
697+
/// Recursively validates inner scope items, such as static variables and constants.
698+
fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
699+
let body = self.db.body(body_id);
700+
for (_, block_def_map) in body.blocks(self.db.upcast()) {
701+
for (_, module) in block_def_map.modules() {
702+
for def_id in module.scope.declarations() {
703+
self.validate_item(def_id);
704+
}
705+
}
706+
}
707+
}
710708
}

crates/hir/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,14 @@ impl ModuleDef {
378378
ModuleDef::BuiltinType(_) | ModuleDef::Macro(_) => return Vec::new(),
379379
};
380380

381-
let module = match self.module(db) {
382-
Some(it) => it,
383-
None => return Vec::new(),
384-
};
385-
386381
let mut acc = Vec::new();
387382

388383
match self.as_def_with_body() {
389384
Some(def) => {
390385
def.diagnostics(db, &mut acc);
391386
}
392387
None => {
393-
for diag in hir_ty::diagnostics::incorrect_case(db, module.id.krate(), id) {
388+
for diag in hir_ty::diagnostics::incorrect_case(db, id) {
394389
acc.push(diag.into())
395390
}
396391
}
@@ -1820,7 +1815,7 @@ impl DefWithBody {
18201815
// FIXME: don't ignore diagnostics for in type const
18211816
DefWithBody::InTypeConst(_) => return,
18221817
};
1823-
for diag in hir_ty::diagnostics::incorrect_case(db, krate, def.into()) {
1818+
for diag in hir_ty::diagnostics::incorrect_case(db, def.into()) {
18241819
acc.push(diag.into())
18251820
}
18261821
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,4 +545,33 @@ pub static SomeStatic: u8 = 10;
545545
"#,
546546
);
547547
}
548+
549+
#[test]
550+
fn fn_inner_items() {
551+
check_diagnostics(
552+
r#"
553+
fn main() {
554+
const foo: bool = true;
555+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
556+
static bar: bool = true;
557+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
558+
fn BAZ() {
559+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
560+
const foo: bool = true;
561+
//^^^ 💡 warn: Constant `foo` should have UPPER_SNAKE_CASE name, e.g. `FOO`
562+
static bar: bool = true;
563+
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
564+
fn BAZ() {
565+
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
566+
let INNER_INNER = 42;
567+
//^^^^^^^^^^^ 💡 warn: Variable `INNER_INNER` should have snake_case name, e.g. `inner_inner`
568+
}
569+
570+
let INNER_LOCAL = 42;
571+
//^^^^^^^^^^^ 💡 warn: Variable `INNER_LOCAL` should have snake_case name, e.g. `inner_local`
572+
}
573+
}
574+
"#,
575+
);
576+
}
548577
}

0 commit comments

Comments
 (0)