Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #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)
bors 2023-11-15
parent 535eb0d · parent 498f39e · commit 57ef70c
-rw-r--r--crates/hir-ty/src/diagnostics/decl_check.rs28
-rw-r--r--crates/hir/src/lib.rs4
-rw-r--r--crates/ide-diagnostics/src/handlers/incorrect_case.rs8
-rw-r--r--crates/ide-diagnostics/src/handlers/type_mismatch.rs15
4 files changed, 22 insertions, 33 deletions
diff --git a/crates/hir-ty/src/diagnostics/decl_check.rs b/crates/hir-ty/src/diagnostics/decl_check.rs
index b432588b23..f4c079b48c 100644
--- a/crates/hir-ty/src/diagnostics/decl_check.rs
+++ b/crates/hir-ty/src/diagnostics/decl_check.rs
@@ -19,8 +19,8 @@ use hir_def::{
data::adt::VariantData,
hir::{Pat, PatId},
src::HasSource,
- AdtId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ItemContainerId,
- Lookup, ModuleDefId, ModuleId, StaticId, StructId,
+ AdtId, AttrDefId, ConstId, EnumId, FunctionId, ItemContainerId, Lookup, ModuleDefId, ModuleId,
+ StaticId, StructId,
};
use hir_expand::{
name::{AsName, Name},
@@ -290,8 +290,6 @@ impl<'a> DeclValidator<'a> {
return;
}
- self.validate_body_inner_items(func.into());
-
// Check whether non-snake case identifiers are allowed for this function.
if self.allowed(func.into(), allow::NON_SNAKE_CASE, false) {
return;
@@ -568,11 +566,6 @@ impl<'a> DeclValidator<'a> {
fn validate_enum(&mut self, enum_id: EnumId) {
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.
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES, false) {
return;
@@ -697,8 +690,6 @@ impl<'a> DeclValidator<'a> {
fn validate_const(&mut self, const_id: ConstId) {
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) {
return;
}
@@ -747,8 +738,6 @@ impl<'a> DeclValidator<'a> {
return;
}
- self.validate_body_inner_items(static_id.into());
-
if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL, false) {
return;
}
@@ -786,17 +775,4 @@ impl<'a> DeclValidator<'a> {
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.
- fn validate_body_inner_items(&mut self, body_id: DefWithBodyId) {
- let body = self.db.body(body_id);
- for (_, block_def_map) in body.blocks(self.db.upcast()) {
- for (_, module) in block_def_map.modules() {
- for def_id in module.scope.declarations() {
- self.validate_item(def_id);
- }
- }
- }
- }
}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index e840c71205..bdc11aa356 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1537,9 +1537,7 @@ impl DefWithBody {
let (body, source_map) = db.body_with_source_map(self.into());
for (_, def_map) in body.blocks(db.upcast()) {
- for diag in def_map.diagnostics() {
- emit_def_diagnostic(db, acc, diag);
- }
+ Module { id: def_map.module_id(DefMap::ROOT) }.diagnostics(db, acc);
}
for diag in source_map.diagnostics() {
diff --git a/crates/ide-diagnostics/src/handlers/incorrect_case.rs b/crates/ide-diagnostics/src/handlers/incorrect_case.rs
index 85dbb7e6f2..0f12e814ba 100644
--- a/crates/ide-diagnostics/src/handlers/incorrect_case.rs
+++ b/crates/ide-diagnostics/src/handlers/incorrect_case.rs
@@ -599,12 +599,12 @@ fn main() {
//^^^ 💡 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`
- let INNER_INNER = 42;
- //^^^^^^^^^^^ 💡 warn: Variable `INNER_INNER` should have snake_case name, e.g. `inner_inner`
+ let _INNER_INNER = 42;
+ //^^^^^^^^^^^^ 💡 warn: Variable `_INNER_INNER` should have snake_case name, e.g. `_inner_inner`
}
- let INNER_LOCAL = 42;
- //^^^^^^^^^^^ 💡 warn: Variable `INNER_LOCAL` should have snake_case name, e.g. `inner_local`
+ let _INNER_LOCAL = 42;
+ //^^^^^^^^^^^^ 💡 warn: Variable `_INNER_LOCAL` should have snake_case name, e.g. `_inner_local`
}
}
"#,
diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
index 16d8ed64ad..c92d92ceae 100644
--- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs
+++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
@@ -742,4 +742,19 @@ fn g() { return; }
"#,
);
}
+
+ #[test]
+ fn smoke_test_inner_items() {
+ check_diagnostics(
+ r#"
+fn f() {
+ fn inner() -> i32 {
+ return;
+ // ^^^^^^ error: expected i32, found ()
+ 0
+ }
+}
+"#,
+ );
+ }
}