mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Proper emit diagnostic without fix
This commit is contained in:
parent
e55e6da13a
commit
a4c9babedb
1 changed files with 23 additions and 17 deletions
|
@ -64,30 +64,36 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.on::<hir::diagnostics::MissingFields, _>(|d| {
|
.on::<hir::diagnostics::MissingFields, _>(|d| {
|
||||||
let mut field_list = d.ast(db);
|
// Note that although we could add a diagnostics to
|
||||||
for f in d.missed_fields.iter() {
|
// fill the missing tuple field, e.g :
|
||||||
// Note that although we could add a diagnostics to
|
// `struct A(usize);`
|
||||||
// fill the missing tuple field, e.g :
|
// `let a = A { 0: () }`
|
||||||
// `struct A(usize);`
|
// but it is uncommon usage and it should not be encouraged.
|
||||||
// `let a = A { 0: () }`
|
let fix = if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
|
||||||
// but it is uncommon usage and it should not be encouraged.
|
None
|
||||||
if f.as_tuple_index().is_some() {
|
} else {
|
||||||
continue;
|
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 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();
|
let mut builder = TextEditBuilder::default();
|
||||||
algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
|
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 {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range: d.highlight_range(),
|
range: d.highlight_range(),
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix: Some(fix),
|
fix,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
|
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue