aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src/diagnostics.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 13:48:54 +0100
committerAleksey Kladov <[email protected]>2021-06-13 13:48:54 +0100
commit6383252cc2770545505d40217732f14e93a396c4 (patch)
treec68f9e02b8ef19b6d4bb70451c202bccbf5858f0 /crates/hir/src/diagnostics.rs
parentc6509a4592b67acc4a99a7ffd6dd688bc6cd29be (diff)
internal: unified missing fields diagnostic
Diffstat (limited to 'crates/hir/src/diagnostics.rs')
-rw-r--r--crates/hir/src/diagnostics.rs52
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 @@
6use std::any::Any; 6use std::any::Any;
7 7
8use cfg::{CfgExpr, CfgOptions, DnfExpr}; 8use cfg::{CfgExpr, CfgOptions, DnfExpr};
9use either::Either;
9use hir_def::path::ModPath; 10use hir_def::path::ModPath;
10use hir_expand::{name::Name, HirFileId, InFile}; 11use hir_expand::{name::Name, HirFileId, InFile};
11use stdx::format_to; 12use stdx::format_to;
@@ -324,60 +325,11 @@ impl Diagnostic for MissingUnsafe {
324#[derive(Debug)] 325#[derive(Debug)]
325pub struct MissingFields { 326pub 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)]
348pub 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
355impl 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(..)`.