Skip to content

Commit 57ef70c

Browse files
committed
Auto merge of rust-lang#15901 - Veykril:inner-diag, r=lnicola
fix: Diagnose everything in nested items, not just def diagnostics Turns out we only calculated def diagnostics for these before (was wondering why I wasn't getting any type mismatches)
2 parents 535eb0d + 498f39e commit 57ef70c

File tree

4 files changed

+22
-33
lines changed

4 files changed

+22
-33
lines changed

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

+2-26
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use hir_def::{
1919
data::adt::VariantData,
2020
hir::{Pat, PatId},
2121
src::HasSource,
22-
AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ItemContainerId,
23-
Lookup, ModuleDefId, ModuleId, StaticId, StructId,
22+
AdtId, AttrDefId, ConstId, EnumId, FunctionId, ItemContainerId, Lookup, ModuleDefId, ModuleId,
23+
StaticId, StructId,
2424
};
2525
use hir_expand::{
2626
name::{AsName, Name},
@@ -290,8 +290,6 @@ impl<'a> DeclValidator<'a> {
290290
return;
291291
}
292292

293-
self.validate_body_inner_items(func.into());
294-
295293
// Check whether non-snake case identifiers are allowed for this function.
296294
if self.allowed(func.into(), allow::NON_SNAKE_CASE, false) {
297295
return;
@@ -568,11 +566,6 @@ impl<'a> DeclValidator<'a> {
568566
fn validate_enum(&mut self, enum_id: EnumId) {
569567
let data = self.db.enum_data(enum_id);
570568

571-
for (local_id, _) in data.variants.iter() {
572-
let variant_id = EnumVariantId { parent: enum_id, local_id };
573-
self.validate_body_inner_items(variant_id.into());
574-
}
575-
576569
// Check whether non-camel case names are allowed for this enum.
577570
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
578571
return;
@@ -697,8 +690,6 @@ impl<'a> DeclValidator<'a> {
697690
fn validate_const(&mut self, const_id: ConstId) {
698691
let data = self.db.const_data(const_id);
699692

700-
self.validate_body_inner_items(const_id.into());
701-
702693
if self.allowed(const_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
703694
return;
704695
}
@@ -747,8 +738,6 @@ impl<'a> DeclValidator<'a> {
747738
return;
748739
}
749740

750-
self.validate_body_inner_items(static_id.into());
751-
752741
if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
753742
return;
754743
}
@@ -786,17 +775,4 @@ impl<'a> DeclValidator<'a> {
786775

787776
self.sink.push(diagnostic);
788777
}
789-
790-
// FIXME: We don't currently validate names within `DefWithBodyId::InTypeConstId`.
791-
/// Recursively validates inner scope items, such as static variables and constants.
792-
fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
793-
let body = self.db.body(body_id);
794-
for (_, block_def_map) in body.blocks(self.db.upcast()) {
795-
for (_, module) in block_def_map.modules() {
796-
for def_id in module.scope.declarations() {
797-
self.validate_item(def_id);
798-
}
799-
}
800-
}
801-
}
802778
}

crates/hir/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,7 @@ impl DefWithBody {
15371537
let (body, source_map) = db.body_with_source_map(self.into());
15381538

15391539
for (_, def_map) in body.blocks(db.upcast()) {
1540-
for diag in def_map.diagnostics() {
1541-
emit_def_diagnostic(db, acc, diag);
1542-
}
1540+
Module { id: def_map.module_id(DefMap::ROOT) }.diagnostics(db, acc);
15431541
}
15441542

15451543
for diag in source_map.diagnostics() {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,12 @@ fn main() {
599599
//^^^ 💡 warn: Static variable `bar` should have UPPER_SNAKE_CASE name, e.g. `BAR`
600600
fn BAZ() {
601601
//^^^ 💡 warn: Function `BAZ` should have snake_case name, e.g. `baz`
602-
let INNER_INNER = 42;
603-
//^^^^^^^^^^^ 💡 warn: Variable `INNER_INNER` should have snake_case name, e.g. `inner_inner`
602+
let _INNER_INNER = 42;
603+
//^^^^^^^^^^^^ 💡 warn: Variable `_INNER_INNER` should have snake_case name, e.g. `_inner_inner`
604604
}
605605
606-
let INNER_LOCAL = 42;
607-
//^^^^^^^^^^^ 💡 warn: Variable `INNER_LOCAL` should have snake_case name, e.g. `inner_local`
606+
let _INNER_LOCAL = 42;
607+
//^^^^^^^^^^^^ 💡 warn: Variable `_INNER_LOCAL` should have snake_case name, e.g. `_inner_local`
608608
}
609609
}
610610
"#,

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

+15
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,21 @@ fn f() -> i32 {
739739
0
740740
}
741741
fn g() { return; }
742+
"#,
743+
);
744+
}
745+
746+
#[test]
747+
fn smoke_test_inner_items() {
748+
check_diagnostics(
749+
r#"
750+
fn f() {
751+
fn inner() -> i32 {
752+
return;
753+
// ^^^^^^ error: expected i32, found ()
754+
0
755+
}
756+
}
742757
"#,
743758
);
744759
}

0 commit comments

Comments
 (0)