Skip to content

Commit 5d8f2bd

Browse files
authored
Merge pull request #2762 from edwin0cheng/fix-2759
Reject tuple index for missing fields assist
2 parents d5abe4c + a4c9bab commit 5d8f2bd

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

crates/ra_ide/src/diagnostics.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,36 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
6464
})
6565
})
6666
.on::<hir::diagnostics::MissingFields, _>(|d| {
67-
let mut field_list = d.ast(db);
68-
for f in d.missed_fields.iter() {
69-
let field = make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit()));
70-
field_list = field_list.append_field(&field);
71-
}
72-
73-
let mut builder = TextEditBuilder::default();
74-
algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
67+
// Note that although we could add a diagnostics to
68+
// fill the missing tuple field, e.g :
69+
// `struct A(usize);`
70+
// `let a = A { 0: () }`
71+
// but it is uncommon usage and it should not be encouraged.
72+
let fix = if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
73+
None
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+
};
7591

76-
let fix =
77-
SourceChange::source_file_edit_from("fill struct fields", file_id, builder.finish());
7892
res.borrow_mut().push(Diagnostic {
7993
range: d.highlight_range(),
8094
message: d.message(),
8195
severity: Severity::Error,
82-
fix: Some(fix),
96+
fix,
8397
})
8498
})
8599
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {

0 commit comments

Comments
 (0)