diff options
Diffstat (limited to 'crates/hir/src/diagnostics.rs')
-rw-r--r-- | crates/hir/src/diagnostics.rs | 95 |
1 files changed, 4 insertions, 91 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 5e2f94698..158626dc0 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs | |||
@@ -6,6 +6,7 @@ | |||
6 | use std::any::Any; | 6 | use std::any::Any; |
7 | 7 | ||
8 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; | 8 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; |
9 | use either::Either; | ||
9 | use hir_def::path::ModPath; | 10 | use hir_def::path::ModPath; |
10 | use hir_expand::{name::Name, HirFileId, InFile}; | 11 | use hir_expand::{name::Name, HirFileId, InFile}; |
11 | use stdx::format_to; | 12 | use stdx::format_to; |
@@ -16,7 +17,7 @@ pub use crate::diagnostics_sink::{ | |||
16 | }; | 17 | }; |
17 | 18 | ||
18 | macro_rules! diagnostics { | 19 | macro_rules! diagnostics { |
19 | ($($diag:ident)*) => { | 20 | ($($diag:ident),*) => { |
20 | pub enum AnyDiagnostic {$( | 21 | pub enum AnyDiagnostic {$( |
21 | $diag(Box<$diag>), | 22 | $diag(Box<$diag>), |
22 | )*} | 23 | )*} |
@@ -31,7 +32,7 @@ macro_rules! diagnostics { | |||
31 | }; | 32 | }; |
32 | } | 33 | } |
33 | 34 | ||
34 | diagnostics![UnresolvedModule]; | 35 | diagnostics![UnresolvedModule, MissingFields]; |
35 | 36 | ||
36 | #[derive(Debug)] | 37 | #[derive(Debug)] |
37 | pub struct UnresolvedModule { | 38 | pub struct UnresolvedModule { |
@@ -321,102 +322,14 @@ impl Diagnostic for MissingUnsafe { | |||
321 | } | 322 | } |
322 | } | 323 | } |
323 | 324 | ||
324 | // Diagnostic: missing-structure-fields | ||
325 | // | ||
326 | // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. | ||
327 | // | ||
328 | // Example: | ||
329 | // | ||
330 | // ```rust | ||
331 | // struct A { a: u8, b: u8 } | ||
332 | // | ||
333 | // let a = A { a: 10 }; | ||
334 | // ``` | ||
335 | #[derive(Debug)] | 325 | #[derive(Debug)] |
336 | pub struct MissingFields { | 326 | pub struct MissingFields { |
337 | pub file: HirFileId, | 327 | pub file: HirFileId, |
338 | pub field_list_parent: AstPtr<ast::RecordExpr>, | 328 | pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>, |
339 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, | ||
340 | pub missed_fields: Vec<Name>, | ||
341 | } | ||
342 | |||
343 | impl Diagnostic for MissingFields { | ||
344 | fn code(&self) -> DiagnosticCode { | ||
345 | DiagnosticCode("missing-structure-fields") | ||
346 | } | ||
347 | fn message(&self) -> String { | ||
348 | let mut buf = String::from("Missing structure fields:\n"); | ||
349 | for field in &self.missed_fields { | ||
350 | format_to!(buf, "- {}\n", field); | ||
351 | } | ||
352 | buf | ||
353 | } | ||
354 | |||
355 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
356 | InFile { | ||
357 | file_id: self.file, | ||
358 | value: self | ||
359 | .field_list_parent_path | ||
360 | .clone() | ||
361 | .map(SyntaxNodePtr::from) | ||
362 | .unwrap_or_else(|| self.field_list_parent.clone().into()), | ||
363 | } | ||
364 | } | ||
365 | |||
366 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
367 | self | ||
368 | } | ||
369 | } | ||
370 | |||
371 | // Diagnostic: missing-pat-fields | ||
372 | // | ||
373 | // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. | ||
374 | // | ||
375 | // Example: | ||
376 | // | ||
377 | // ```rust | ||
378 | // struct A { a: u8, b: u8 } | ||
379 | // | ||
380 | // let a = A { a: 10, b: 20 }; | ||
381 | // | ||
382 | // if let A { a } = a { | ||
383 | // // ... | ||
384 | // } | ||
385 | // ``` | ||
386 | #[derive(Debug)] | ||
387 | pub struct MissingPatFields { | ||
388 | pub file: HirFileId, | ||
389 | pub field_list_parent: AstPtr<ast::RecordPat>, | ||
390 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, | 329 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, |
391 | pub missed_fields: Vec<Name>, | 330 | pub missed_fields: Vec<Name>, |
392 | } | 331 | } |
393 | 332 | ||
394 | impl Diagnostic for MissingPatFields { | ||
395 | fn code(&self) -> DiagnosticCode { | ||
396 | DiagnosticCode("missing-pat-fields") | ||
397 | } | ||
398 | fn message(&self) -> String { | ||
399 | let mut buf = String::from("Missing structure fields:\n"); | ||
400 | for field in &self.missed_fields { | ||
401 | format_to!(buf, "- {}\n", field); | ||
402 | } | ||
403 | buf | ||
404 | } | ||
405 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
406 | InFile { | ||
407 | file_id: self.file, | ||
408 | value: self | ||
409 | .field_list_parent_path | ||
410 | .clone() | ||
411 | .map(SyntaxNodePtr::from) | ||
412 | .unwrap_or_else(|| self.field_list_parent.clone().into()), | ||
413 | } | ||
414 | } | ||
415 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
416 | self | ||
417 | } | ||
418 | } | ||
419 | |||
420 | // Diagnostic: replace-filter-map-next-with-find-map | 333 | // Diagnostic: replace-filter-map-next-with-find-map |
421 | // | 334 | // |
422 | // This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. | 335 | // This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. |