2 use hir::{db::AstDatabase, InFile};
3 use ide_assists::Assist;
4 use ide_db::source_change::SourceChange;
6 use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
7 use text_edit::TextEdit;
9 use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
11 // Diagnostic: missing-fields
13 // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
18 // struct A { a: u8, b: u8 }
20 // let a = A { a: 10 };
22 pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic {
23 let mut message = String::from("Missing structure fields:\n");
24 for field in &d.missed_fields {
25 format_to!(message, "- {}\n", field);
28 let ptr = InFile::new(
30 d.field_list_parent_path
32 .map(SyntaxNodePtr::from)
33 .unwrap_or_else(|| d.field_list_parent.clone().either(|it| it.into(), |it| it.into())),
36 Diagnostic::new("missing-fields", message, ctx.sema.diagnostics_display_range(ptr).range)
37 .with_fixes(fixes(ctx, d))
40 fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Assist>> {
41 // Note that although we could add a diagnostics to
42 // fill the missing tuple field, e.g :
44 // `let a = A { 0: () }`
45 // but it is uncommon usage and it should not be encouraged.
46 if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
50 let root = ctx.sema.db.parse_or_expand(d.file)?;
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,
56 let old_field_list = field_list_parent.record_expr_field_list()?;
57 let new_field_list = old_field_list.clone_for_update();
58 for f in d.missed_fields.iter() {
60 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
62 new_field_list.add_field(field);
66 let mut builder = TextEdit::builder();
67 algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder);
71 "fill_missing_fields",
73 SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
74 ctx.sema.original_range(field_list_parent.syntax()).range,
80 use crate::diagnostics::tests::{check_diagnostics, check_fix};
83 fn missing_record_pat_field_diagnostic() {
86 struct S { foo: i32, bar: () }
89 //^ Missing structure fields:
97 fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() {
100 struct S { foo: i32, bar: () }
101 fn baz(s: S) -> i32 {
103 S { foo, .. } => foo,
111 fn missing_record_pat_field_box() {
114 struct S { s: Box<u32> }
123 fn missing_record_pat_field_ref() {
135 fn range_mapping_out_of_macros() {
136 // FIXME: this is very wrong, but somewhat tricky to fix.
143 macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
146 let _x = id![Foo { a: $042 }];
149 pub struct Foo { pub a: i32, pub b: i32 }
156 macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
159 let _x = id![Foo { a: 42 }];
162 pub struct Foo { pub a: i32, pub b: i32 }
168 fn test_fill_struct_fields_empty() {
171 struct TestStruct { one: i32, two: i64 }
174 let s = TestStruct {$0};
178 struct TestStruct { one: i32, two: i64 }
181 let s = TestStruct { one: (), two: () };
188 fn test_fill_struct_fields_self() {
191 struct TestStruct { one: i32 }
194 fn test_fn() { let s = Self {$0}; }
198 struct TestStruct { one: i32 }
201 fn test_fn() { let s = Self { one: () }; }
208 fn test_fill_struct_fields_enum() {
212 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
216 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
223 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
227 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
228 Expr::Bin { lhs: (), rhs: () }
236 fn test_fill_struct_fields_partial() {
239 struct TestStruct { one: i32, two: i64 }
242 let s = TestStruct{ two: 2$0 };
246 struct TestStruct { one: i32, two: i64 }
249 let s = TestStruct{ two: 2, one: () };
256 fn test_fill_struct_fields_raw_ident() {
259 struct TestStruct { r#type: u8 }
266 struct TestStruct { r#type: u8 }
269 TestStruct { r#type: () };
276 fn test_fill_struct_fields_no_diagnostic() {
279 struct TestStruct { one: i32, two: i64 }
283 let s = TestStruct{ one, two: 2 };
290 fn test_fill_struct_fields_no_diagnostic_on_spread() {
293 struct TestStruct { one: i32, two: i64 }
297 let s = TestStruct{ ..a };
304 fn test_fill_struct_fields_blank_line() {
307 struct S { a: (), b: () }
316 struct S { a: (), b: () }