diff options
author | Aleksey Kladov <[email protected]> | 2021-06-13 13:48:54 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-13 13:48:54 +0100 |
commit | 6383252cc2770545505d40217732f14e93a396c4 (patch) | |
tree | c68f9e02b8ef19b6d4bb70451c202bccbf5858f0 /crates/hir/src/diagnostics.rs | |
parent | c6509a4592b67acc4a99a7ffd6dd688bc6cd29be (diff) |
internal: unified missing fields diagnostic
Diffstat (limited to 'crates/hir/src/diagnostics.rs')
-rw-r--r-- | crates/hir/src/diagnostics.rs | 52 |
1 files changed, 2 insertions, 50 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index a5e982b75..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; |
@@ -324,60 +325,11 @@ impl Diagnostic for MissingUnsafe { | |||
324 | #[derive(Debug)] | 325 | #[derive(Debug)] |
325 | pub struct MissingFields { | 326 | pub struct MissingFields { |
326 | pub file: HirFileId, | 327 | pub file: HirFileId, |
327 | pub field_list_parent: AstPtr<ast::RecordExpr>, | 328 | pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>, |
328 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, | 329 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, |
329 | pub missed_fields: Vec<Name>, | 330 | pub missed_fields: Vec<Name>, |
330 | } | 331 | } |
331 | 332 | ||
332 | // Diagnostic: missing-pat-fields | ||
333 | // | ||
334 | // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. | ||
335 | // | ||
336 | // Example: | ||
337 | // | ||
338 | // ```rust | ||
339 | // struct A { a: u8, b: u8 } | ||
340 | // | ||
341 | // let a = A { a: 10, b: 20 }; | ||
342 | // | ||
343 | // if let A { a } = a { | ||
344 | // // ... | ||
345 | // } | ||
346 | // ``` | ||
347 | #[derive(Debug)] | ||
348 | pub struct MissingPatFields { | ||
349 | pub file: HirFileId, | ||
350 | pub field_list_parent: AstPtr<ast::RecordPat>, | ||
351 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, | ||
352 | pub missed_fields: Vec<Name>, | ||
353 | } | ||
354 | |||
355 | impl Diagnostic for MissingPatFields { | ||
356 | fn code(&self) -> DiagnosticCode { | ||
357 | DiagnosticCode("missing-pat-fields") | ||
358 | } | ||
359 | fn message(&self) -> String { | ||
360 | let mut buf = String::from("Missing structure fields:\n"); | ||
361 | for field in &self.missed_fields { | ||
362 | format_to!(buf, "- {}\n", field); | ||
363 | } | ||
364 | buf | ||
365 | } | ||
366 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
367 | InFile { | ||
368 | file_id: self.file, | ||
369 | value: self | ||
370 | .field_list_parent_path | ||
371 | .clone() | ||
372 | .map(SyntaxNodePtr::from) | ||
373 | .unwrap_or_else(|| self.field_list_parent.clone().into()), | ||
374 | } | ||
375 | } | ||
376 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
377 | self | ||
378 | } | ||
379 | } | ||
380 | |||
381 | // Diagnostic: replace-filter-map-next-with-find-map | 333 | // Diagnostic: replace-filter-map-next-with-find-map |
382 | // | 334 | // |
383 | // 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(..)`. |