From 6383252cc2770545505d40217732f14e93a396c4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 15:48:54 +0300 Subject: internal: unified missing fields diagnostic --- crates/ide/src/diagnostics/missing_fields.rs | 35 ++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/missing_fields.rs b/crates/ide/src/diagnostics/missing_fields.rs index aee780972..66575f713 100644 --- a/crates/ide/src/diagnostics/missing_fields.rs +++ b/crates/ide/src/diagnostics/missing_fields.rs @@ -1,3 +1,4 @@ +use either::Either; use hir::{db::AstDatabase, InFile}; use ide_assists::Assist; use ide_db::source_change::SourceChange; @@ -7,7 +8,7 @@ use text_edit::TextEdit; use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext}; -// Diagnostic: missing-structure-fields +// Diagnostic: missing-fields // // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. // @@ -29,15 +30,11 @@ pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingField d.field_list_parent_path .clone() .map(SyntaxNodePtr::from) - .unwrap_or_else(|| d.field_list_parent.clone().into()), + .unwrap_or_else(|| d.field_list_parent.clone().either(|it| it.into(), |it| it.into())), ); - Diagnostic::new( - "missing-structure-fields", - message, - ctx.sema.diagnostics_display_range(ptr).range, - ) - .with_fixes(fixes(ctx, d)) + Diagnostic::new("missing-fields", message, ctx.sema.diagnostics_display_range(ptr).range) + .with_fixes(fixes(ctx, d)) } fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option> { @@ -51,7 +48,11 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option record_expr.to_node(&root), + // FIXE: patterns should be fixable as well. + Either::Right(_) => return None, + }; let old_field_list = field_list_parent.record_expr_field_list()?; let new_field_list = old_field_list.clone_for_update(); for f in d.missed_fields.iter() { @@ -76,7 +77,21 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option