diff options
Diffstat (limited to 'crates/ide/src/diagnostics')
-rw-r--r-- | crates/ide/src/diagnostics/fixes.rs | 1 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/fixes/fill_missing_fields.rs | 217 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/missing_fields.rs | 256 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/unresolved_module.rs | 5 |
4 files changed, 260 insertions, 219 deletions
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 8640d7231..a2e792b3b 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs | |||
@@ -2,7 +2,6 @@ | |||
2 | //! The same module also has all curret custom fixes for the diagnostics implemented. | 2 | //! The same module also has all curret custom fixes for the diagnostics implemented. |
3 | mod change_case; | 3 | mod change_case; |
4 | mod create_field; | 4 | mod create_field; |
5 | mod fill_missing_fields; | ||
6 | mod remove_semicolon; | 5 | mod remove_semicolon; |
7 | mod replace_with_find_map; | 6 | mod replace_with_find_map; |
8 | mod wrap_tail_expr; | 7 | mod wrap_tail_expr; |
diff --git a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs b/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs deleted file mode 100644 index c76f6008a..000000000 --- a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs +++ /dev/null | |||
@@ -1,217 +0,0 @@ | |||
1 | use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics}; | ||
2 | use ide_assists::AssistResolveStrategy; | ||
3 | use ide_db::{source_change::SourceChange, RootDatabase}; | ||
4 | use syntax::{algo, ast::make, AstNode}; | ||
5 | use text_edit::TextEdit; | ||
6 | |||
7 | use crate::{ | ||
8 | diagnostics::{fix, fixes::DiagnosticWithFixes}, | ||
9 | Assist, | ||
10 | }; | ||
11 | |||
12 | impl DiagnosticWithFixes for MissingFields { | ||
13 | fn fixes( | ||
14 | &self, | ||
15 | sema: &Semantics<RootDatabase>, | ||
16 | _resolve: &AssistResolveStrategy, | ||
17 | ) -> Option<Vec<Assist>> { | ||
18 | // Note that although we could add a diagnostics to | ||
19 | // fill the missing tuple field, e.g : | ||
20 | // `struct A(usize);` | ||
21 | // `let a = A { 0: () }` | ||
22 | // but it is uncommon usage and it should not be encouraged. | ||
23 | if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { | ||
24 | return None; | ||
25 | } | ||
26 | |||
27 | let root = sema.db.parse_or_expand(self.file)?; | ||
28 | let field_list_parent = self.field_list_parent.to_node(&root); | ||
29 | let old_field_list = field_list_parent.record_expr_field_list()?; | ||
30 | let new_field_list = old_field_list.clone_for_update(); | ||
31 | for f in self.missed_fields.iter() { | ||
32 | let field = | ||
33 | make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit())) | ||
34 | .clone_for_update(); | ||
35 | new_field_list.add_field(field); | ||
36 | } | ||
37 | |||
38 | let edit = { | ||
39 | let mut builder = TextEdit::builder(); | ||
40 | algo::diff(old_field_list.syntax(), new_field_list.syntax()) | ||
41 | .into_text_edit(&mut builder); | ||
42 | builder.finish() | ||
43 | }; | ||
44 | Some(vec![fix( | ||
45 | "fill_missing_fields", | ||
46 | "Fill struct fields", | ||
47 | SourceChange::from_text_edit(self.file.original_file(sema.db), edit), | ||
48 | sema.original_range(field_list_parent.syntax()).range, | ||
49 | )]) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | #[cfg(test)] | ||
54 | mod tests { | ||
55 | use crate::diagnostics::tests::{check_fix, check_no_diagnostics}; | ||
56 | |||
57 | #[test] | ||
58 | fn test_fill_struct_fields_empty() { | ||
59 | check_fix( | ||
60 | r#" | ||
61 | struct TestStruct { one: i32, two: i64 } | ||
62 | |||
63 | fn test_fn() { | ||
64 | let s = TestStruct {$0}; | ||
65 | } | ||
66 | "#, | ||
67 | r#" | ||
68 | struct TestStruct { one: i32, two: i64 } | ||
69 | |||
70 | fn test_fn() { | ||
71 | let s = TestStruct { one: (), two: () }; | ||
72 | } | ||
73 | "#, | ||
74 | ); | ||
75 | } | ||
76 | |||
77 | #[test] | ||
78 | fn test_fill_struct_fields_self() { | ||
79 | check_fix( | ||
80 | r#" | ||
81 | struct TestStruct { one: i32 } | ||
82 | |||
83 | impl TestStruct { | ||
84 | fn test_fn() { let s = Self {$0}; } | ||
85 | } | ||
86 | "#, | ||
87 | r#" | ||
88 | struct TestStruct { one: i32 } | ||
89 | |||
90 | impl TestStruct { | ||
91 | fn test_fn() { let s = Self { one: () }; } | ||
92 | } | ||
93 | "#, | ||
94 | ); | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn test_fill_struct_fields_enum() { | ||
99 | check_fix( | ||
100 | r#" | ||
101 | enum Expr { | ||
102 | Bin { lhs: Box<Expr>, rhs: Box<Expr> } | ||
103 | } | ||
104 | |||
105 | impl Expr { | ||
106 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | ||
107 | Expr::Bin {$0 } | ||
108 | } | ||
109 | } | ||
110 | "#, | ||
111 | r#" | ||
112 | enum Expr { | ||
113 | Bin { lhs: Box<Expr>, rhs: Box<Expr> } | ||
114 | } | ||
115 | |||
116 | impl Expr { | ||
117 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | ||
118 | Expr::Bin { lhs: (), rhs: () } | ||
119 | } | ||
120 | } | ||
121 | "#, | ||
122 | ); | ||
123 | } | ||
124 | |||
125 | #[test] | ||
126 | fn test_fill_struct_fields_partial() { | ||
127 | check_fix( | ||
128 | r#" | ||
129 | struct TestStruct { one: i32, two: i64 } | ||
130 | |||
131 | fn test_fn() { | ||
132 | let s = TestStruct{ two: 2$0 }; | ||
133 | } | ||
134 | "#, | ||
135 | r" | ||
136 | struct TestStruct { one: i32, two: i64 } | ||
137 | |||
138 | fn test_fn() { | ||
139 | let s = TestStruct{ two: 2, one: () }; | ||
140 | } | ||
141 | ", | ||
142 | ); | ||
143 | } | ||
144 | |||
145 | #[test] | ||
146 | fn test_fill_struct_fields_raw_ident() { | ||
147 | check_fix( | ||
148 | r#" | ||
149 | struct TestStruct { r#type: u8 } | ||
150 | |||
151 | fn test_fn() { | ||
152 | TestStruct { $0 }; | ||
153 | } | ||
154 | "#, | ||
155 | r" | ||
156 | struct TestStruct { r#type: u8 } | ||
157 | |||
158 | fn test_fn() { | ||
159 | TestStruct { r#type: () }; | ||
160 | } | ||
161 | ", | ||
162 | ); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn test_fill_struct_fields_no_diagnostic() { | ||
167 | check_no_diagnostics( | ||
168 | r#" | ||
169 | struct TestStruct { one: i32, two: i64 } | ||
170 | |||
171 | fn test_fn() { | ||
172 | let one = 1; | ||
173 | let s = TestStruct{ one, two: 2 }; | ||
174 | } | ||
175 | "#, | ||
176 | ); | ||
177 | } | ||
178 | |||
179 | #[test] | ||
180 | fn test_fill_struct_fields_no_diagnostic_on_spread() { | ||
181 | check_no_diagnostics( | ||
182 | r#" | ||
183 | struct TestStruct { one: i32, two: i64 } | ||
184 | |||
185 | fn test_fn() { | ||
186 | let one = 1; | ||
187 | let s = TestStruct{ ..a }; | ||
188 | } | ||
189 | "#, | ||
190 | ); | ||
191 | } | ||
192 | |||
193 | #[test] | ||
194 | fn test_fill_struct_fields_blank_line() { | ||
195 | check_fix( | ||
196 | r#" | ||
197 | struct S { a: (), b: () } | ||
198 | |||
199 | fn f() { | ||
200 | S { | ||
201 | $0 | ||
202 | }; | ||
203 | } | ||
204 | "#, | ||
205 | r#" | ||
206 | struct S { a: (), b: () } | ||
207 | |||
208 | fn f() { | ||
209 | S { | ||
210 | a: (), | ||
211 | b: (), | ||
212 | }; | ||
213 | } | ||
214 | "#, | ||
215 | ); | ||
216 | } | ||
217 | } | ||
diff --git a/crates/ide/src/diagnostics/missing_fields.rs b/crates/ide/src/diagnostics/missing_fields.rs new file mode 100644 index 000000000..66575f713 --- /dev/null +++ b/crates/ide/src/diagnostics/missing_fields.rs | |||
@@ -0,0 +1,256 @@ | |||
1 | use either::Either; | ||
2 | use hir::{db::AstDatabase, InFile}; | ||
3 | use ide_assists::Assist; | ||
4 | use ide_db::source_change::SourceChange; | ||
5 | use stdx::format_to; | ||
6 | use syntax::{algo, ast::make, AstNode, SyntaxNodePtr}; | ||
7 | use text_edit::TextEdit; | ||
8 | |||
9 | use 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 | // ``` | ||
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); | ||
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 | |||
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 : | ||
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)] | ||
79 | mod tests { | ||
80 | use crate::diagnostics::tests::{check_diagnostics, check_fix, check_no_diagnostics}; | ||
81 | |||
82 | #[test] | ||
83 | fn missing_record_pat_field_diagnostic() { | ||
84 | check_diagnostics( | ||
85 | r#" | ||
86 | struct S { foo: i32, bar: () } | ||
87 | fn baz(s: S) { | ||
88 | let S { foo: _ } = s; | ||
89 | //^ Missing structure fields: | ||
90 | //| - bar | ||
91 | } | ||
92 | "#, | ||
93 | ); | ||
94 | } | ||
95 | |||
96 | #[test] | ||
97 | fn test_fill_struct_fields_empty() { | ||
98 | check_fix( | ||
99 | r#" | ||
100 | struct TestStruct { one: i32, two: i64 } | ||
101 | |||
102 | fn test_fn() { | ||
103 | let s = TestStruct {$0}; | ||
104 | } | ||
105 | "#, | ||
106 | r#" | ||
107 | struct TestStruct { one: i32, two: i64 } | ||
108 | |||
109 | fn test_fn() { | ||
110 | let s = TestStruct { one: (), two: () }; | ||
111 | } | ||
112 | "#, | ||
113 | ); | ||
114 | } | ||
115 | |||
116 | #[test] | ||
117 | fn test_fill_struct_fields_self() { | ||
118 | check_fix( | ||
119 | r#" | ||
120 | struct TestStruct { one: i32 } | ||
121 | |||
122 | impl TestStruct { | ||
123 | fn test_fn() { let s = Self {$0}; } | ||
124 | } | ||
125 | "#, | ||
126 | r#" | ||
127 | struct TestStruct { one: i32 } | ||
128 | |||
129 | impl TestStruct { | ||
130 | fn test_fn() { let s = Self { one: () }; } | ||
131 | } | ||
132 | "#, | ||
133 | ); | ||
134 | } | ||
135 | |||
136 | #[test] | ||
137 | fn test_fill_struct_fields_enum() { | ||
138 | check_fix( | ||
139 | r#" | ||
140 | enum Expr { | ||
141 | Bin { lhs: Box<Expr>, rhs: Box<Expr> } | ||
142 | } | ||
143 | |||
144 | impl Expr { | ||
145 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | ||
146 | Expr::Bin {$0 } | ||
147 | } | ||
148 | } | ||
149 | "#, | ||
150 | r#" | ||
151 | enum Expr { | ||
152 | Bin { lhs: Box<Expr>, rhs: Box<Expr> } | ||
153 | } | ||
154 | |||
155 | impl Expr { | ||
156 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | ||
157 | Expr::Bin { lhs: (), rhs: () } | ||
158 | } | ||
159 | } | ||
160 | "#, | ||
161 | ); | ||
162 | } | ||
163 | |||
164 | #[test] | ||
165 | fn test_fill_struct_fields_partial() { | ||
166 | check_fix( | ||
167 | r#" | ||
168 | struct TestStruct { one: i32, two: i64 } | ||
169 | |||
170 | fn test_fn() { | ||
171 | let s = TestStruct{ two: 2$0 }; | ||
172 | } | ||
173 | "#, | ||
174 | r" | ||
175 | struct TestStruct { one: i32, two: i64 } | ||
176 | |||
177 | fn test_fn() { | ||
178 | let s = TestStruct{ two: 2, one: () }; | ||
179 | } | ||
180 | ", | ||
181 | ); | ||
182 | } | ||
183 | |||
184 | #[test] | ||
185 | fn test_fill_struct_fields_raw_ident() { | ||
186 | check_fix( | ||
187 | r#" | ||
188 | struct TestStruct { r#type: u8 } | ||
189 | |||
190 | fn test_fn() { | ||
191 | TestStruct { $0 }; | ||
192 | } | ||
193 | "#, | ||
194 | r" | ||
195 | struct TestStruct { r#type: u8 } | ||
196 | |||
197 | fn test_fn() { | ||
198 | TestStruct { r#type: () }; | ||
199 | } | ||
200 | ", | ||
201 | ); | ||
202 | } | ||
203 | |||
204 | #[test] | ||
205 | fn test_fill_struct_fields_no_diagnostic() { | ||
206 | check_no_diagnostics( | ||
207 | r#" | ||
208 | struct TestStruct { one: i32, two: i64 } | ||
209 | |||
210 | fn test_fn() { | ||
211 | let one = 1; | ||
212 | let s = TestStruct{ one, two: 2 }; | ||
213 | } | ||
214 | "#, | ||
215 | ); | ||
216 | } | ||
217 | |||
218 | #[test] | ||
219 | fn test_fill_struct_fields_no_diagnostic_on_spread() { | ||
220 | check_no_diagnostics( | ||
221 | r#" | ||
222 | struct TestStruct { one: i32, two: i64 } | ||
223 | |||
224 | fn test_fn() { | ||
225 | let one = 1; | ||
226 | let s = TestStruct{ ..a }; | ||
227 | } | ||
228 | "#, | ||
229 | ); | ||
230 | } | ||
231 | |||
232 | #[test] | ||
233 | fn test_fill_struct_fields_blank_line() { | ||
234 | check_fix( | ||
235 | r#" | ||
236 | struct S { a: (), b: () } | ||
237 | |||
238 | fn f() { | ||
239 | S { | ||
240 | $0 | ||
241 | }; | ||
242 | } | ||
243 | "#, | ||
244 | r#" | ||
245 | struct S { a: (), b: () } | ||
246 | |||
247 | fn f() { | ||
248 | S { | ||
249 | a: (), | ||
250 | b: (), | ||
251 | }; | ||
252 | } | ||
253 | "#, | ||
254 | ); | ||
255 | } | ||
256 | } | ||
diff --git a/crates/ide/src/diagnostics/unresolved_module.rs b/crates/ide/src/diagnostics/unresolved_module.rs index abf53a57c..4c8c74ff7 100644 --- a/crates/ide/src/diagnostics/unresolved_module.rs +++ b/crates/ide/src/diagnostics/unresolved_module.rs | |||
@@ -8,7 +8,10 @@ use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext}; | |||
8 | // Diagnostic: unresolved-module | 8 | // Diagnostic: unresolved-module |
9 | // | 9 | // |
10 | // This diagnostic is triggered if rust-analyzer is unable to discover referred module. | 10 | // This diagnostic is triggered if rust-analyzer is unable to discover referred module. |
11 | pub(super) fn render(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Diagnostic { | 11 | pub(super) fn unresolved_module( |
12 | ctx: &DiagnosticsContext<'_>, | ||
13 | d: &hir::UnresolvedModule, | ||
14 | ) -> Diagnostic { | ||
12 | Diagnostic::new( | 15 | Diagnostic::new( |
13 | "unresolved-module", | 16 | "unresolved-module", |
14 | "unresolved module", | 17 | "unresolved module", |