aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-13 13:49:37 +0100
committerGitHub <[email protected]>2021-06-13 13:49:37 +0100
commitcc6d761a99ab3b7e28ed13ca3839358f3341da4d (patch)
treed9d180d516ddbafc7eb950f401e9a8ab3f1e88fe /crates/ide
parent3f53a5dd724cbc7aa20280cddba44c7d2c0c8a6d (diff)
parent6383252cc2770545505d40217732f14e93a396c4 (diff)
Merge #9246
9246: internal: unified missing fields diagnostic r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/diagnostics.rs21
-rw-r--r--crates/ide/src/diagnostics/fixes.rs1
-rw-r--r--crates/ide/src/diagnostics/fixes/fill_missing_fields.rs217
-rw-r--r--crates/ide/src/diagnostics/missing_fields.rs256
-rw-r--r--crates/ide/src/diagnostics/unresolved_module.rs5
5 files changed, 263 insertions, 237 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 075aae8d5..3307e240b 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -5,6 +5,7 @@
5//! original files. So we need to map the ranges. 5//! original files. So we need to map the ranges.
6 6
7mod unresolved_module; 7mod unresolved_module;
8mod missing_fields;
8 9
9mod fixes; 10mod fixes;
10mod field_shorthand; 11mod field_shorthand;
@@ -123,9 +124,6 @@ pub(crate) fn diagnostics(
123 } 124 }
124 let res = RefCell::new(res); 125 let res = RefCell::new(res);
125 let sink_builder = DiagnosticSinkBuilder::new() 126 let sink_builder = DiagnosticSinkBuilder::new()
126 .on::<hir::diagnostics::MissingFields, _>(|d| {
127 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
128 })
129 .on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| { 127 .on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| {
130 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve)); 128 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
131 }) 129 })
@@ -232,7 +230,8 @@ pub(crate) fn diagnostics(
232 let ctx = DiagnosticsContext { config, sema, resolve }; 230 let ctx = DiagnosticsContext { config, sema, resolve };
233 for diag in diags { 231 for diag in diags {
234 let d = match diag { 232 let d = match diag {
235 AnyDiagnostic::UnresolvedModule(d) => unresolved_module::render(&ctx, &d), 233 AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
234 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
236 }; 235 };
237 if let Some(code) = d.code { 236 if let Some(code) = d.code {
238 if ctx.config.disabled.contains(code.as_str()) { 237 if ctx.config.disabled.contains(code.as_str()) {
@@ -1057,20 +1056,6 @@ fn main() {
1057 } 1056 }
1058 1057
1059 #[test] 1058 #[test]
1060 fn missing_record_pat_field_diagnostic() {
1061 check_diagnostics(
1062 r#"
1063struct S { foo: i32, bar: () }
1064fn baz(s: S) {
1065 let S { foo: _ } = s;
1066 //^ Missing structure fields:
1067 //| - bar
1068}
1069"#,
1070 );
1071 }
1072
1073 #[test]
1074 fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() { 1059 fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() {
1075 check_diagnostics( 1060 check_diagnostics(
1076 r" 1061 r"
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.
3mod change_case; 3mod change_case;
4mod create_field; 4mod create_field;
5mod fill_missing_fields;
6mod remove_semicolon; 5mod remove_semicolon;
7mod replace_with_find_map; 6mod replace_with_find_map;
8mod wrap_tail_expr; 7mod 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 @@
1use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics};
2use ide_assists::AssistResolveStrategy;
3use ide_db::{source_change::SourceChange, RootDatabase};
4use syntax::{algo, ast::make, AstNode};
5use text_edit::TextEdit;
6
7use crate::{
8 diagnostics::{fix, fixes::DiagnosticWithFixes},
9 Assist,
10};
11
12impl 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)]
54mod 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#"
61struct TestStruct { one: i32, two: i64 }
62
63fn test_fn() {
64 let s = TestStruct {$0};
65}
66"#,
67 r#"
68struct TestStruct { one: i32, two: i64 }
69
70fn 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#"
81struct TestStruct { one: i32 }
82
83impl TestStruct {
84 fn test_fn() { let s = Self {$0}; }
85}
86"#,
87 r#"
88struct TestStruct { one: i32 }
89
90impl 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#"
101enum Expr {
102 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
103}
104
105impl Expr {
106 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
107 Expr::Bin {$0 }
108 }
109}
110"#,
111 r#"
112enum Expr {
113 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
114}
115
116impl 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#"
129struct TestStruct { one: i32, two: i64 }
130
131fn test_fn() {
132 let s = TestStruct{ two: 2$0 };
133}
134"#,
135 r"
136struct TestStruct { one: i32, two: i64 }
137
138fn 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#"
149struct TestStruct { r#type: u8 }
150
151fn test_fn() {
152 TestStruct { $0 };
153}
154"#,
155 r"
156struct TestStruct { r#type: u8 }
157
158fn 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#"
169struct TestStruct { one: i32, two: i64 }
170
171fn 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#"
183struct TestStruct { one: i32, two: i64 }
184
185fn 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#"
197struct S { a: (), b: () }
198
199fn f() {
200 S {
201 $0
202 };
203}
204"#,
205 r#"
206struct S { a: (), b: () }
207
208fn 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 @@
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, check_no_diagnostics};
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 test_fill_struct_fields_empty() {
98 check_fix(
99 r#"
100struct TestStruct { one: i32, two: i64 }
101
102fn test_fn() {
103 let s = TestStruct {$0};
104}
105"#,
106 r#"
107struct TestStruct { one: i32, two: i64 }
108
109fn 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#"
120struct TestStruct { one: i32 }
121
122impl TestStruct {
123 fn test_fn() { let s = Self {$0}; }
124}
125"#,
126 r#"
127struct TestStruct { one: i32 }
128
129impl 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#"
140enum Expr {
141 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
142}
143
144impl Expr {
145 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
146 Expr::Bin {$0 }
147 }
148}
149"#,
150 r#"
151enum Expr {
152 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
153}
154
155impl 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#"
168struct TestStruct { one: i32, two: i64 }
169
170fn test_fn() {
171 let s = TestStruct{ two: 2$0 };
172}
173"#,
174 r"
175struct TestStruct { one: i32, two: i64 }
176
177fn 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#"
188struct TestStruct { r#type: u8 }
189
190fn test_fn() {
191 TestStruct { $0 };
192}
193"#,
194 r"
195struct TestStruct { r#type: u8 }
196
197fn 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#"
208struct TestStruct { one: i32, two: i64 }
209
210fn 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#"
222struct TestStruct { one: i32, two: i64 }
223
224fn 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#"
236struct S { a: (), b: () }
237
238fn f() {
239 S {
240 $0
241 };
242}
243"#,
244 r#"
245struct S { a: (), b: () }
246
247fn 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.
11pub(super) fn render(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Diagnostic { 11pub(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",