aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-08 11:49:54 +0000
committerGitHub <[email protected]>2020-01-08 11:49:54 +0000
commit5d8f2bd822c1e9384ef547c781ccc26a6dec63e2 (patch)
tree77d7aa8fd4ba146327322d052c0991bb87c5e26b /crates/ra_ide
parentd5abe4cfddd5c6e14100f20d27d3de38fe7201f5 (diff)
parenta4c9babedb6f19b9dbf065f59761e1c804642189 (diff)
Merge pull request #2762 from edwin0cheng/fix-2759
Reject tuple index for missing fields assist
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/diagnostics.rs36
1 files changed, 25 insertions, 11 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs
index c50a70d99..478368529 100644
--- a/crates/ra_ide/src/diagnostics.rs
+++ b/crates/ra_ide/src/diagnostics.rs
@@ -64,22 +64,36 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
64 }) 64 })
65 }) 65 })
66 .on::<hir::diagnostics::MissingFields, _>(|d| { 66 .on::<hir::diagnostics::MissingFields, _>(|d| {
67 let mut field_list = d.ast(db); 67 // Note that although we could add a diagnostics to
68 for f in d.missed_fields.iter() { 68 // fill the missing tuple field, e.g :
69 let field = make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); 69 // `struct A(usize);`
70 field_list = field_list.append_field(&field); 70 // `let a = A { 0: () }`
71 } 71 // but it is uncommon usage and it should not be encouraged.
72 72 let fix = if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
73 let mut builder = TextEditBuilder::default(); 73 None
74 algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder); 74 } else {
75 let mut field_list = d.ast(db);
76 for f in d.missed_fields.iter() {
77 let field =
78 make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit()));
79 field_list = field_list.append_field(&field);
80 }
81
82 let mut builder = TextEditBuilder::default();
83 algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
84
85 Some(SourceChange::source_file_edit_from(
86 "fill struct fields",
87 file_id,
88 builder.finish(),
89 ))
90 };
75 91
76 let fix =
77 SourceChange::source_file_edit_from("fill struct fields", file_id, builder.finish());
78 res.borrow_mut().push(Diagnostic { 92 res.borrow_mut().push(Diagnostic {
79 range: d.highlight_range(), 93 range: d.highlight_range(),
80 message: d.message(), 94 message: d.message(),
81 severity: Severity::Error, 95 severity: Severity::Error,
82 fix: Some(fix), 96 fix,
83 }) 97 })
84 }) 98 })
85 .on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| { 99 .on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {