From e55e6da13adb41d84a53927b9bf02dc03fd2a33a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 8 Jan 2020 02:08:17 +0800 Subject: Reject tuple index for missing fields assist --- crates/ra_ide/src/diagnostics.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index c50a70d99..3264f8f80 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -66,6 +66,14 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec .on::(|d| { let mut field_list = d.ast(db); for f in d.missed_fields.iter() { + // Note that although we could add a diagnostics to + // fill the missing tuple field, e.g : + // `struct A(usize);` + // `let a = A { 0: () }` + // but it is uncommon usage and it should not be encouraged. + if f.as_tuple_index().is_some() { + continue; + } let field = make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); field_list = field_list.append_field(&field); } -- cgit v1.2.3 From a4c9babedb6f19b9dbf065f59761e1c804642189 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 8 Jan 2020 02:17:58 +0800 Subject: Proper emit diagnostic without fix --- crates/ra_ide/src/diagnostics.rs | 44 +++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 3264f8f80..478368529 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -64,30 +64,36 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec }) }) .on::(|d| { - let mut field_list = d.ast(db); - for f in d.missed_fields.iter() { - // Note that although we could add a diagnostics to - // fill the missing tuple field, e.g : - // `struct A(usize);` - // `let a = A { 0: () }` - // but it is uncommon usage and it should not be encouraged. - if f.as_tuple_index().is_some() { - continue; - } - let field = make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); - field_list = field_list.append_field(&field); - } - - let mut builder = TextEditBuilder::default(); - algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder); + // Note that although we could add a diagnostics to + // fill the missing tuple field, e.g : + // `struct A(usize);` + // `let a = A { 0: () }` + // but it is uncommon usage and it should not be encouraged. + let fix = if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { + None + } else { + let mut field_list = d.ast(db); + for f in d.missed_fields.iter() { + let field = + make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); + field_list = field_list.append_field(&field); + } + + let mut builder = TextEditBuilder::default(); + algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder); + + Some(SourceChange::source_file_edit_from( + "fill struct fields", + file_id, + builder.finish(), + )) + }; - let fix = - SourceChange::source_file_edit_from("fill struct fields", file_id, builder.finish()); res.borrow_mut().push(Diagnostic { range: d.highlight_range(), message: d.message(), severity: Severity::Error, - fix: Some(fix), + fix, }) }) .on::(|d| { -- cgit v1.2.3