aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics/missing_fields.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-14 11:15:05 +0100
committerAleksey Kladov <[email protected]>2021-06-14 15:45:17 +0100
commit1d2772c2c7dc0a42d8a9429d24ea41412add61b3 (patch)
tree2e727c6465f972b7f62857bc1143e08f4b4416d4 /crates/ide/src/diagnostics/missing_fields.rs
parent3d2f0400a26ef6b07d61a06e1b543072b627570e (diff)
internal: move diagnostics to a new crate
Diffstat (limited to 'crates/ide/src/diagnostics/missing_fields.rs')
-rw-r--r--crates/ide/src/diagnostics/missing_fields.rs327
1 files changed, 0 insertions, 327 deletions
diff --git a/crates/ide/src/diagnostics/missing_fields.rs b/crates/ide/src/diagnostics/missing_fields.rs
deleted file mode 100644
index d01f05041..000000000
--- a/crates/ide/src/diagnostics/missing_fields.rs
+++ /dev/null
@@ -1,327 +0,0 @@
1use either::Either;
2use hir::{db::AstDatabase, InFile};
3use ide_assists::Assist;
4use ide_db::source_change::SourceChange;
5use stdx::format_to;
6use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
7use text_edit::TextEdit;
8
9use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
10
11// Diagnostic: missing-fields
12//
13// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
14//
15// Example:
16//
17// ```rust
18// struct A { a: u8, b: u8 }
19//
20// let a = A { a: 10 };
21// ```
22pub(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);
26 }
27
28 let ptr = InFile::new(
29 d.file,
30 d.field_list_parent_path
31 .clone()
32 .map(SyntaxNodePtr::from)
33 .unwrap_or_else(|| d.field_list_parent.clone().either(|it| it.into(), |it| it.into())),
34 );
35
36 Diagnostic::new("missing-fields", message, ctx.sema.diagnostics_display_range(ptr).range)
37 .with_fixes(fixes(ctx, d))
38}
39
40fn 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 :
43 // `struct A(usize);`
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()) {
47 return None;
48 }
49
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,
55 };
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() {
59 let field =
60 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
61 .clone_for_update();
62 new_field_list.add_field(field);
63 }
64
65 let edit = {
66 let mut builder = TextEdit::builder();
67 algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder);
68 builder.finish()
69 };
70 Some(vec![fix(
71 "fill_missing_fields",
72 "Fill struct fields",
73 SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
74 ctx.sema.original_range(field_list_parent.syntax()).range,
75 )])
76}
77
78#[cfg(test)]
79mod tests {
80 use crate::diagnostics::tests::{check_diagnostics, check_fix};
81
82 #[test]
83 fn missing_record_pat_field_diagnostic() {
84 check_diagnostics(
85 r#"
86struct S { foo: i32, bar: () }
87fn baz(s: S) {
88 let S { foo: _ } = s;
89 //^ Missing structure fields:
90 //| - bar
91}
92"#,
93 );
94 }
95
96 #[test]
97 fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() {
98 check_diagnostics(
99 r"
100struct S { foo: i32, bar: () }
101fn baz(s: S) -> i32 {
102 match s {
103 S { foo, .. } => foo,
104 }
105}
106",
107 )
108 }
109
110 #[test]
111 fn missing_record_pat_field_box() {
112 check_diagnostics(
113 r"
114struct S { s: Box<u32> }
115fn x(a: S) {
116 let S { box s } = a;
117}
118",
119 )
120 }
121
122 #[test]
123 fn missing_record_pat_field_ref() {
124 check_diagnostics(
125 r"
126struct S { s: u32 }
127fn x(a: S) {
128 let S { ref s } = a;
129}
130",
131 )
132 }
133
134 #[test]
135 fn range_mapping_out_of_macros() {
136 // FIXME: this is very wrong, but somewhat tricky to fix.
137 check_fix(
138 r#"
139fn some() {}
140fn items() {}
141fn here() {}
142
143macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
144
145fn main() {
146 let _x = id![Foo { a: $042 }];
147}
148
149pub struct Foo { pub a: i32, pub b: i32 }
150"#,
151 r#"
152fn some(, b: () ) {}
153fn items() {}
154fn here() {}
155
156macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
157
158fn main() {
159 let _x = id![Foo { a: 42 }];
160}
161
162pub struct Foo { pub a: i32, pub b: i32 }
163"#,
164 );
165 }
166
167 #[test]
168 fn test_fill_struct_fields_empty() {
169 check_fix(
170 r#"
171struct TestStruct { one: i32, two: i64 }
172
173fn test_fn() {
174 let s = TestStruct {$0};
175}
176"#,
177 r#"
178struct TestStruct { one: i32, two: i64 }
179
180fn test_fn() {
181 let s = TestStruct { one: (), two: () };
182}
183"#,
184 );
185 }
186
187 #[test]
188 fn test_fill_struct_fields_self() {
189 check_fix(
190 r#"
191struct TestStruct { one: i32 }
192
193impl TestStruct {
194 fn test_fn() { let s = Self {$0}; }
195}
196"#,
197 r#"
198struct TestStruct { one: i32 }
199
200impl TestStruct {
201 fn test_fn() { let s = Self { one: () }; }
202}
203"#,
204 );
205 }
206
207 #[test]
208 fn test_fill_struct_fields_enum() {
209 check_fix(
210 r#"
211enum Expr {
212 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
213}
214
215impl Expr {
216 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
217 Expr::Bin {$0 }
218 }
219}
220"#,
221 r#"
222enum Expr {
223 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
224}
225
226impl Expr {
227 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
228 Expr::Bin { lhs: (), rhs: () }
229 }
230}
231"#,
232 );
233 }
234
235 #[test]
236 fn test_fill_struct_fields_partial() {
237 check_fix(
238 r#"
239struct TestStruct { one: i32, two: i64 }
240
241fn test_fn() {
242 let s = TestStruct{ two: 2$0 };
243}
244"#,
245 r"
246struct TestStruct { one: i32, two: i64 }
247
248fn test_fn() {
249 let s = TestStruct{ two: 2, one: () };
250}
251",
252 );
253 }
254
255 #[test]
256 fn test_fill_struct_fields_raw_ident() {
257 check_fix(
258 r#"
259struct TestStruct { r#type: u8 }
260
261fn test_fn() {
262 TestStruct { $0 };
263}
264"#,
265 r"
266struct TestStruct { r#type: u8 }
267
268fn test_fn() {
269 TestStruct { r#type: () };
270}
271",
272 );
273 }
274
275 #[test]
276 fn test_fill_struct_fields_no_diagnostic() {
277 check_diagnostics(
278 r#"
279struct TestStruct { one: i32, two: i64 }
280
281fn test_fn() {
282 let one = 1;
283 let s = TestStruct{ one, two: 2 };
284}
285 "#,
286 );
287 }
288
289 #[test]
290 fn test_fill_struct_fields_no_diagnostic_on_spread() {
291 check_diagnostics(
292 r#"
293struct TestStruct { one: i32, two: i64 }
294
295fn test_fn() {
296 let one = 1;
297 let s = TestStruct{ ..a };
298}
299"#,
300 );
301 }
302
303 #[test]
304 fn test_fill_struct_fields_blank_line() {
305 check_fix(
306 r#"
307struct S { a: (), b: () }
308
309fn f() {
310 S {
311 $0
312 };
313}
314"#,
315 r#"
316struct S { a: (), b: () }
317
318fn f() {
319 S {
320 a: (),
321 b: (),
322 };
323}
324"#,
325 );
326 }
327}