From c6509a4592b67acc4a99a7ffd6dd688bc6cd29be Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 15:27:15 +0300 Subject: internal: move missing_fields diagnostics --- crates/hir/src/diagnostics.rs | 43 ++----------------------------------------- crates/hir/src/lib.rs | 33 ++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 56 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 5e2f94698..a5e982b75 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -16,7 +16,7 @@ pub use crate::diagnostics_sink::{ }; macro_rules! diagnostics { - ($($diag:ident)*) => { + ($($diag:ident),*) => { pub enum AnyDiagnostic {$( $diag(Box<$diag>), )*} @@ -31,7 +31,7 @@ macro_rules! diagnostics { }; } -diagnostics![UnresolvedModule]; +diagnostics![UnresolvedModule, MissingFields]; #[derive(Debug)] pub struct UnresolvedModule { @@ -321,17 +321,6 @@ impl Diagnostic for MissingUnsafe { } } -// Diagnostic: missing-structure-fields -// -// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. -// -// Example: -// -// ```rust -// struct A { a: u8, b: u8 } -// -// let a = A { a: 10 }; -// ``` #[derive(Debug)] pub struct MissingFields { pub file: HirFileId, @@ -340,34 +329,6 @@ pub struct MissingFields { pub missed_fields: Vec, } -impl Diagnostic for MissingFields { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("missing-structure-fields") - } - fn message(&self) -> String { - let mut buf = String::from("Missing structure fields:\n"); - for field in &self.missed_fields { - format_to!(buf, "- {}\n", field); - } - buf - } - - fn display_source(&self) -> InFile { - InFile { - file_id: self.file, - value: self - .field_list_parent_path - .clone() - .map(SyntaxNodePtr::from) - .unwrap_or_else(|| self.field_list_parent.clone().into()), - } - } - - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } -} - // Diagnostic: missing-pat-fields // // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index ff6c68dbc..583d92f20 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -609,23 +609,21 @@ impl Module { } for decl in self.declarations(db) { match decl { - crate::ModuleDef::Function(f) => f.diagnostics(db, sink, internal_diagnostics), - crate::ModuleDef::Module(m) => { + ModuleDef::Function(f) => acc.extend(f.diagnostics(db, sink, internal_diagnostics)), + ModuleDef::Module(m) => { // Only add diagnostics from inline modules if def_map[m.id.local_id].origin.is_inline() { acc.extend(m.diagnostics(db, sink, internal_diagnostics)) } } - _ => { - decl.diagnostics(db, sink); - } + _ => decl.diagnostics(db, sink), } } for impl_def in self.impl_defs(db) { for item in impl_def.items(db) { if let AssocItem::Function(f) = item { - f.diagnostics(db, sink, internal_diagnostics); + acc.extend(f.diagnostics(db, sink, internal_diagnostics)); } } } @@ -1033,7 +1031,8 @@ impl Function { db: &dyn HirDatabase, sink: &mut DiagnosticSink, internal_diagnostics: bool, - ) { + ) -> Vec { + let mut acc: Vec = Vec::new(); let krate = self.module(db).id.krate(); let source_map = db.body_with_source_map(self.id.into()).1; @@ -1114,14 +1113,17 @@ impl Function { .into_iter() .map(|idx| variant_data.fields()[idx].name.clone()) .collect(); - sink.push(MissingFields { - file: source_ptr.file_id, - field_list_parent: AstPtr::new(record_expr), - field_list_parent_path: record_expr - .path() - .map(|path| AstPtr::new(&path)), - missed_fields, - }) + acc.push( + MissingFields { + file: source_ptr.file_id, + field_list_parent: AstPtr::new(record_expr), + field_list_parent_path: record_expr + .path() + .map(|path| AstPtr::new(&path)), + missed_fields, + } + .into(), + ) } } } @@ -1234,6 +1236,7 @@ impl Function { for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) { sink.push(diag) } + acc } /// Whether this function declaration has a definition. -- cgit v1.2.3