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/ide/src | |
parent | c6509a4592b67acc4a99a7ffd6dd688bc6cd29be (diff) |
internal: unified missing fields diagnostic
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 14 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/missing_fields.rs | 35 |
2 files changed, 25 insertions, 24 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index ef8c8044c..3307e240b 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -1056,20 +1056,6 @@ fn main() { | |||
1056 | } | 1056 | } |
1057 | 1057 | ||
1058 | #[test] | 1058 | #[test] |
1059 | fn missing_record_pat_field_diagnostic() { | ||
1060 | check_diagnostics( | ||
1061 | r#" | ||
1062 | struct S { foo: i32, bar: () } | ||
1063 | fn baz(s: S) { | ||
1064 | let S { foo: _ } = s; | ||
1065 | //^ Missing structure fields: | ||
1066 | //| - bar | ||
1067 | } | ||
1068 | "#, | ||
1069 | ); | ||
1070 | } | ||
1071 | |||
1072 | #[test] | ||
1073 | fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() { | 1059 | fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() { |
1074 | check_diagnostics( | 1060 | check_diagnostics( |
1075 | r" | 1061 | r" |
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 @@ | |||
1 | use either::Either; | ||
1 | use hir::{db::AstDatabase, InFile}; | 2 | use hir::{db::AstDatabase, InFile}; |
2 | use ide_assists::Assist; | 3 | use ide_assists::Assist; |
3 | use ide_db::source_change::SourceChange; | 4 | use ide_db::source_change::SourceChange; |
@@ -7,7 +8,7 @@ use text_edit::TextEdit; | |||
7 | 8 | ||
8 | use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext}; | 9 | use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext}; |
9 | 10 | ||
10 | // Diagnostic: missing-structure-fields | 11 | // Diagnostic: missing-fields |
11 | // | 12 | // |
12 | // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. | 13 | // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. |
13 | // | 14 | // |
@@ -29,15 +30,11 @@ pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingField | |||
29 | d.field_list_parent_path | 30 | d.field_list_parent_path |
30 | .clone() | 31 | .clone() |
31 | .map(SyntaxNodePtr::from) | 32 | .map(SyntaxNodePtr::from) |
32 | .unwrap_or_else(|| d.field_list_parent.clone().into()), | 33 | .unwrap_or_else(|| d.field_list_parent.clone().either(|it| it.into(), |it| it.into())), |
33 | ); | 34 | ); |
34 | 35 | ||
35 | Diagnostic::new( | 36 | Diagnostic::new("missing-fields", message, ctx.sema.diagnostics_display_range(ptr).range) |
36 | "missing-structure-fields", | 37 | .with_fixes(fixes(ctx, d)) |
37 | message, | ||
38 | ctx.sema.diagnostics_display_range(ptr).range, | ||
39 | ) | ||
40 | .with_fixes(fixes(ctx, d)) | ||
41 | } | 38 | } |
42 | 39 | ||
43 | fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Assist>> { | 40 | fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Assist>> { |
@@ -51,7 +48,11 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass | |||
51 | } | 48 | } |
52 | 49 | ||
53 | let root = ctx.sema.db.parse_or_expand(d.file)?; | 50 | let root = ctx.sema.db.parse_or_expand(d.file)?; |
54 | let field_list_parent = d.field_list_parent.to_node(&root); | 51 | let field_list_parent = match &d.field_list_parent { |
52 | Either::Left(record_expr) => record_expr.to_node(&root), | ||
53 | // FIXE: patterns should be fixable as well. | ||
54 | Either::Right(_) => return None, | ||
55 | }; | ||
55 | let old_field_list = field_list_parent.record_expr_field_list()?; | 56 | let old_field_list = field_list_parent.record_expr_field_list()?; |
56 | let new_field_list = old_field_list.clone_for_update(); | 57 | let new_field_list = old_field_list.clone_for_update(); |
57 | for f in d.missed_fields.iter() { | 58 | for f in d.missed_fields.iter() { |
@@ -76,7 +77,21 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass | |||
76 | 77 | ||
77 | #[cfg(test)] | 78 | #[cfg(test)] |
78 | mod tests { | 79 | mod tests { |
79 | use crate::diagnostics::tests::{check_fix, check_no_diagnostics}; | 80 | use crate::diagnostics::tests::{check_diagnostics, check_fix, check_no_diagnostics}; |
81 | |||
82 | #[test] | ||
83 | fn missing_record_pat_field_diagnostic() { | ||
84 | check_diagnostics( | ||
85 | r#" | ||
86 | struct S { foo: i32, bar: () } | ||
87 | fn baz(s: S) { | ||
88 | let S { foo: _ } = s; | ||
89 | //^ Missing structure fields: | ||
90 | //| - bar | ||
91 | } | ||
92 | "#, | ||
93 | ); | ||
94 | } | ||
80 | 95 | ||
81 | #[test] | 96 | #[test] |
82 | fn test_fill_struct_fields_empty() { | 97 | fn test_fill_struct_fields_empty() { |