diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:19:53 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:20:13 +0100 |
commit | 7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch) | |
tree | bdb47765991cb973b2cd5481a088fac636bd326c /crates/ra_ide/src/diagnostics.rs | |
parent | ca464650eeaca6195891199a93f4f76cf3e7e697 (diff) | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/ra_ide/src/diagnostics.rs')
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 797 |
1 files changed, 0 insertions, 797 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs deleted file mode 100644 index dd8a7ffd9..000000000 --- a/crates/ra_ide/src/diagnostics.rs +++ /dev/null | |||
@@ -1,797 +0,0 @@ | |||
1 | //! Collects diagnostics & fixits for a single file. | ||
2 | //! | ||
3 | //! The tricky bit here is that diagnostics are produced by hir in terms of | ||
4 | //! macro-expanded files, but we need to present them to the users in terms of | ||
5 | //! original files. So we need to map the ranges. | ||
6 | |||
7 | use std::cell::RefCell; | ||
8 | |||
9 | use hir::{ | ||
10 | diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSinkBuilder}, | ||
11 | HasSource, HirDisplay, Semantics, VariantDef, | ||
12 | }; | ||
13 | use itertools::Itertools; | ||
14 | use ra_db::SourceDatabase; | ||
15 | use ra_ide_db::RootDatabase; | ||
16 | use ra_prof::profile; | ||
17 | use ra_syntax::{ | ||
18 | algo, | ||
19 | ast::{self, edit::IndentLevel, make, AstNode}, | ||
20 | SyntaxNode, TextRange, T, | ||
21 | }; | ||
22 | use ra_text_edit::{TextEdit, TextEditBuilder}; | ||
23 | |||
24 | use crate::{Diagnostic, FileId, FileSystemEdit, Fix, SourceFileEdit}; | ||
25 | |||
26 | #[derive(Debug, Copy, Clone)] | ||
27 | pub enum Severity { | ||
28 | Error, | ||
29 | WeakWarning, | ||
30 | } | ||
31 | |||
32 | pub(crate) fn diagnostics( | ||
33 | db: &RootDatabase, | ||
34 | file_id: FileId, | ||
35 | enable_experimental: bool, | ||
36 | ) -> Vec<Diagnostic> { | ||
37 | let _p = profile("diagnostics"); | ||
38 | let sema = Semantics::new(db); | ||
39 | let parse = db.parse(file_id); | ||
40 | let mut res = Vec::new(); | ||
41 | |||
42 | // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. | ||
43 | res.extend(parse.errors().iter().take(128).map(|err| Diagnostic { | ||
44 | range: err.range(), | ||
45 | message: format!("Syntax Error: {}", err), | ||
46 | severity: Severity::Error, | ||
47 | fix: None, | ||
48 | })); | ||
49 | |||
50 | for node in parse.tree().syntax().descendants() { | ||
51 | check_unnecessary_braces_in_use_statement(&mut res, file_id, &node); | ||
52 | check_struct_shorthand_initialization(&mut res, file_id, &node); | ||
53 | } | ||
54 | let res = RefCell::new(res); | ||
55 | let mut sink = DiagnosticSinkBuilder::new() | ||
56 | .on::<hir::diagnostics::UnresolvedModule, _>(|d| { | ||
57 | let original_file = d.source().file_id.original_file(db); | ||
58 | let fix = Fix::new( | ||
59 | "Create module", | ||
60 | FileSystemEdit::CreateFile { anchor: original_file, dst: d.candidate.clone() } | ||
61 | .into(), | ||
62 | ); | ||
63 | res.borrow_mut().push(Diagnostic { | ||
64 | range: sema.diagnostics_range(d).range, | ||
65 | message: d.message(), | ||
66 | severity: Severity::Error, | ||
67 | fix: Some(fix), | ||
68 | }) | ||
69 | }) | ||
70 | .on::<hir::diagnostics::MissingFields, _>(|d| { | ||
71 | // Note that although we could add a diagnostics to | ||
72 | // fill the missing tuple field, e.g : | ||
73 | // `struct A(usize);` | ||
74 | // `let a = A { 0: () }` | ||
75 | // but it is uncommon usage and it should not be encouraged. | ||
76 | let fix = if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { | ||
77 | None | ||
78 | } else { | ||
79 | let mut field_list = d.ast(db); | ||
80 | for f in d.missed_fields.iter() { | ||
81 | let field = | ||
82 | make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); | ||
83 | field_list = field_list.append_field(&field); | ||
84 | } | ||
85 | |||
86 | let edit = { | ||
87 | let mut builder = TextEditBuilder::default(); | ||
88 | algo::diff(&d.ast(db).syntax(), &field_list.syntax()) | ||
89 | .into_text_edit(&mut builder); | ||
90 | builder.finish() | ||
91 | }; | ||
92 | Some(Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into())) | ||
93 | }; | ||
94 | |||
95 | res.borrow_mut().push(Diagnostic { | ||
96 | range: sema.diagnostics_range(d).range, | ||
97 | message: d.message(), | ||
98 | severity: Severity::Error, | ||
99 | fix, | ||
100 | }) | ||
101 | }) | ||
102 | .on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| { | ||
103 | let node = d.ast(db); | ||
104 | let replacement = format!("Ok({})", node.syntax()); | ||
105 | let edit = TextEdit::replace(node.syntax().text_range(), replacement); | ||
106 | let source_change = SourceFileEdit { file_id, edit }.into(); | ||
107 | let fix = Fix::new("Wrap with ok", source_change); | ||
108 | res.borrow_mut().push(Diagnostic { | ||
109 | range: sema.diagnostics_range(d).range, | ||
110 | message: d.message(), | ||
111 | severity: Severity::Error, | ||
112 | fix: Some(fix), | ||
113 | }) | ||
114 | }) | ||
115 | .on::<hir::diagnostics::NoSuchField, _>(|d| { | ||
116 | res.borrow_mut().push(Diagnostic { | ||
117 | range: sema.diagnostics_range(d).range, | ||
118 | message: d.message(), | ||
119 | severity: Severity::Error, | ||
120 | fix: missing_struct_field_fix(&sema, file_id, d), | ||
121 | }) | ||
122 | }) | ||
123 | // Only collect experimental diagnostics when they're enabled. | ||
124 | .filter(|diag| !diag.is_experimental() || enable_experimental) | ||
125 | // Diagnostics not handled above get no fix and default treatment. | ||
126 | .build(|d| { | ||
127 | res.borrow_mut().push(Diagnostic { | ||
128 | message: d.message(), | ||
129 | range: sema.diagnostics_range(d).range, | ||
130 | severity: Severity::Error, | ||
131 | fix: None, | ||
132 | }) | ||
133 | }); | ||
134 | |||
135 | if let Some(m) = sema.to_module_def(file_id) { | ||
136 | m.diagnostics(db, &mut sink); | ||
137 | }; | ||
138 | drop(sink); | ||
139 | res.into_inner() | ||
140 | } | ||
141 | |||
142 | fn missing_struct_field_fix( | ||
143 | sema: &Semantics<RootDatabase>, | ||
144 | usage_file_id: FileId, | ||
145 | d: &hir::diagnostics::NoSuchField, | ||
146 | ) -> Option<Fix> { | ||
147 | let record_expr = sema.ast(d); | ||
148 | |||
149 | let record_lit = ast::RecordExpr::cast(record_expr.syntax().parent()?.parent()?)?; | ||
150 | let def_id = sema.resolve_variant(record_lit)?; | ||
151 | let module; | ||
152 | let def_file_id; | ||
153 | let record_fields = match VariantDef::from(def_id) { | ||
154 | VariantDef::Struct(s) => { | ||
155 | module = s.module(sema.db); | ||
156 | let source = s.source(sema.db); | ||
157 | def_file_id = source.file_id; | ||
158 | let fields = source.value.field_list()?; | ||
159 | record_field_list(fields)? | ||
160 | } | ||
161 | VariantDef::Union(u) => { | ||
162 | module = u.module(sema.db); | ||
163 | let source = u.source(sema.db); | ||
164 | def_file_id = source.file_id; | ||
165 | source.value.record_field_list()? | ||
166 | } | ||
167 | VariantDef::EnumVariant(e) => { | ||
168 | module = e.module(sema.db); | ||
169 | let source = e.source(sema.db); | ||
170 | def_file_id = source.file_id; | ||
171 | let fields = source.value.field_list()?; | ||
172 | record_field_list(fields)? | ||
173 | } | ||
174 | }; | ||
175 | let def_file_id = def_file_id.original_file(sema.db); | ||
176 | |||
177 | let new_field_type = sema.type_of_expr(&record_expr.expr()?)?; | ||
178 | if new_field_type.is_unknown() { | ||
179 | return None; | ||
180 | } | ||
181 | let new_field = make::record_field_def( | ||
182 | record_expr.field_name()?, | ||
183 | make::type_ref(&new_field_type.display_source_code(sema.db, module.into()).ok()?), | ||
184 | ); | ||
185 | |||
186 | let last_field = record_fields.fields().last()?; | ||
187 | let last_field_syntax = last_field.syntax(); | ||
188 | let indent = IndentLevel::from_node(last_field_syntax); | ||
189 | |||
190 | let mut new_field = new_field.to_string(); | ||
191 | if usage_file_id != def_file_id { | ||
192 | new_field = format!("pub(crate) {}", new_field); | ||
193 | } | ||
194 | new_field = format!("\n{}{}", indent, new_field); | ||
195 | |||
196 | let needs_comma = !last_field_syntax.to_string().ends_with(','); | ||
197 | if needs_comma { | ||
198 | new_field = format!(",{}", new_field); | ||
199 | } | ||
200 | |||
201 | let source_change = SourceFileEdit { | ||
202 | file_id: def_file_id, | ||
203 | edit: TextEdit::insert(last_field_syntax.text_range().end(), new_field), | ||
204 | }; | ||
205 | let fix = Fix::new("Create field", source_change.into()); | ||
206 | return Some(fix); | ||
207 | |||
208 | fn record_field_list(field_def_list: ast::FieldList) -> Option<ast::RecordFieldList> { | ||
209 | match field_def_list { | ||
210 | ast::FieldList::RecordFieldList(it) => Some(it), | ||
211 | ast::FieldList::TupleFieldList(_) => None, | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | |||
216 | fn check_unnecessary_braces_in_use_statement( | ||
217 | acc: &mut Vec<Diagnostic>, | ||
218 | file_id: FileId, | ||
219 | node: &SyntaxNode, | ||
220 | ) -> Option<()> { | ||
221 | let use_tree_list = ast::UseTreeList::cast(node.clone())?; | ||
222 | if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() { | ||
223 | let range = use_tree_list.syntax().text_range(); | ||
224 | let edit = | ||
225 | text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree) | ||
226 | .unwrap_or_else(|| { | ||
227 | let to_replace = single_use_tree.syntax().text().to_string(); | ||
228 | let mut edit_builder = TextEditBuilder::default(); | ||
229 | edit_builder.delete(range); | ||
230 | edit_builder.insert(range.start(), to_replace); | ||
231 | edit_builder.finish() | ||
232 | }); | ||
233 | |||
234 | acc.push(Diagnostic { | ||
235 | range, | ||
236 | message: "Unnecessary braces in use statement".to_string(), | ||
237 | severity: Severity::WeakWarning, | ||
238 | fix: Some(Fix::new( | ||
239 | "Remove unnecessary braces", | ||
240 | SourceFileEdit { file_id, edit }.into(), | ||
241 | )), | ||
242 | }); | ||
243 | } | ||
244 | |||
245 | Some(()) | ||
246 | } | ||
247 | |||
248 | fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( | ||
249 | single_use_tree: &ast::UseTree, | ||
250 | ) -> Option<TextEdit> { | ||
251 | let use_tree_list_node = single_use_tree.syntax().parent()?; | ||
252 | if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] { | ||
253 | let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start(); | ||
254 | let end = use_tree_list_node.text_range().end(); | ||
255 | let range = TextRange::new(start, end); | ||
256 | return Some(TextEdit::delete(range)); | ||
257 | } | ||
258 | None | ||
259 | } | ||
260 | |||
261 | fn check_struct_shorthand_initialization( | ||
262 | acc: &mut Vec<Diagnostic>, | ||
263 | file_id: FileId, | ||
264 | node: &SyntaxNode, | ||
265 | ) -> Option<()> { | ||
266 | let record_lit = ast::RecordExpr::cast(node.clone())?; | ||
267 | let record_field_list = record_lit.record_expr_field_list()?; | ||
268 | for record_field in record_field_list.fields() { | ||
269 | if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { | ||
270 | let field_name = name_ref.syntax().text().to_string(); | ||
271 | let field_expr = expr.syntax().text().to_string(); | ||
272 | let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); | ||
273 | if field_name == field_expr && !field_name_is_tup_index { | ||
274 | let mut edit_builder = TextEditBuilder::default(); | ||
275 | edit_builder.delete(record_field.syntax().text_range()); | ||
276 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); | ||
277 | let edit = edit_builder.finish(); | ||
278 | |||
279 | acc.push(Diagnostic { | ||
280 | range: record_field.syntax().text_range(), | ||
281 | message: "Shorthand struct initialization".to_string(), | ||
282 | severity: Severity::WeakWarning, | ||
283 | fix: Some(Fix::new( | ||
284 | "Use struct shorthand initialization", | ||
285 | SourceFileEdit { file_id, edit }.into(), | ||
286 | )), | ||
287 | }); | ||
288 | } | ||
289 | } | ||
290 | } | ||
291 | Some(()) | ||
292 | } | ||
293 | |||
294 | #[cfg(test)] | ||
295 | mod tests { | ||
296 | use stdx::trim_indent; | ||
297 | use test_utils::assert_eq_text; | ||
298 | |||
299 | use crate::mock_analysis::{analysis_and_position, single_file, MockAnalysis}; | ||
300 | use expect::{expect, Expect}; | ||
301 | |||
302 | /// Takes a multi-file input fixture with annotated cursor positions, | ||
303 | /// and checks that: | ||
304 | /// * a diagnostic is produced | ||
305 | /// * this diagnostic touches the input cursor position | ||
306 | /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied | ||
307 | fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) { | ||
308 | let after = trim_indent(ra_fixture_after); | ||
309 | |||
310 | let (analysis, file_position) = analysis_and_position(ra_fixture_before); | ||
311 | let diagnostic = analysis.diagnostics(file_position.file_id, true).unwrap().pop().unwrap(); | ||
312 | let mut fix = diagnostic.fix.unwrap(); | ||
313 | let edit = fix.source_change.source_file_edits.pop().unwrap().edit; | ||
314 | let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); | ||
315 | let actual = { | ||
316 | let mut actual = target_file_contents.to_string(); | ||
317 | edit.apply(&mut actual); | ||
318 | actual | ||
319 | }; | ||
320 | |||
321 | assert_eq_text!(&after, &actual); | ||
322 | assert!( | ||
323 | diagnostic.range.start() <= file_position.offset | ||
324 | && diagnostic.range.end() >= file_position.offset, | ||
325 | "diagnostic range {:?} does not touch cursor position {:?}", | ||
326 | diagnostic.range, | ||
327 | file_position.offset | ||
328 | ); | ||
329 | } | ||
330 | |||
331 | /// Checks that a diagnostic applies to the file containing the `<|>` cursor marker | ||
332 | /// which has a fix that can apply to other files. | ||
333 | fn check_apply_diagnostic_fix_in_other_file(ra_fixture_before: &str, ra_fixture_after: &str) { | ||
334 | let ra_fixture_after = &trim_indent(ra_fixture_after); | ||
335 | let (analysis, file_pos) = analysis_and_position(ra_fixture_before); | ||
336 | let current_file_id = file_pos.file_id; | ||
337 | let diagnostic = analysis.diagnostics(current_file_id, true).unwrap().pop().unwrap(); | ||
338 | let mut fix = diagnostic.fix.unwrap(); | ||
339 | let edit = fix.source_change.source_file_edits.pop().unwrap(); | ||
340 | let changed_file_id = edit.file_id; | ||
341 | let before = analysis.file_text(changed_file_id).unwrap(); | ||
342 | let actual = { | ||
343 | let mut actual = before.to_string(); | ||
344 | edit.edit.apply(&mut actual); | ||
345 | actual | ||
346 | }; | ||
347 | assert_eq_text!(ra_fixture_after, &actual); | ||
348 | } | ||
349 | |||
350 | /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics | ||
351 | /// apply to the file containing the cursor. | ||
352 | fn check_no_diagnostics(ra_fixture: &str) { | ||
353 | let mock = MockAnalysis::with_files(ra_fixture); | ||
354 | let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>(); | ||
355 | let analysis = mock.analysis(); | ||
356 | let diagnostics = files | ||
357 | .into_iter() | ||
358 | .flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) | ||
359 | .collect::<Vec<_>>(); | ||
360 | assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics); | ||
361 | } | ||
362 | |||
363 | fn check_expect(ra_fixture: &str, expect: Expect) { | ||
364 | let (analysis, file_id) = single_file(ra_fixture); | ||
365 | let diagnostics = analysis.diagnostics(file_id, true).unwrap(); | ||
366 | expect.assert_debug_eq(&diagnostics) | ||
367 | } | ||
368 | |||
369 | #[test] | ||
370 | fn test_wrap_return_type() { | ||
371 | check_fix( | ||
372 | r#" | ||
373 | //- /main.rs | ||
374 | use core::result::Result::{self, Ok, Err}; | ||
375 | |||
376 | fn div(x: i32, y: i32) -> Result<i32, ()> { | ||
377 | if y == 0 { | ||
378 | return Err(()); | ||
379 | } | ||
380 | x / y<|> | ||
381 | } | ||
382 | //- /core/lib.rs | ||
383 | pub mod result { | ||
384 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
385 | } | ||
386 | "#, | ||
387 | r#" | ||
388 | use core::result::Result::{self, Ok, Err}; | ||
389 | |||
390 | fn div(x: i32, y: i32) -> Result<i32, ()> { | ||
391 | if y == 0 { | ||
392 | return Err(()); | ||
393 | } | ||
394 | Ok(x / y) | ||
395 | } | ||
396 | "#, | ||
397 | ); | ||
398 | } | ||
399 | |||
400 | #[test] | ||
401 | fn test_wrap_return_type_handles_generic_functions() { | ||
402 | check_fix( | ||
403 | r#" | ||
404 | //- /main.rs | ||
405 | use core::result::Result::{self, Ok, Err}; | ||
406 | |||
407 | fn div<T>(x: T) -> Result<T, i32> { | ||
408 | if x == 0 { | ||
409 | return Err(7); | ||
410 | } | ||
411 | <|>x | ||
412 | } | ||
413 | //- /core/lib.rs | ||
414 | pub mod result { | ||
415 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
416 | } | ||
417 | "#, | ||
418 | r#" | ||
419 | use core::result::Result::{self, Ok, Err}; | ||
420 | |||
421 | fn div<T>(x: T) -> Result<T, i32> { | ||
422 | if x == 0 { | ||
423 | return Err(7); | ||
424 | } | ||
425 | Ok(x) | ||
426 | } | ||
427 | "#, | ||
428 | ); | ||
429 | } | ||
430 | |||
431 | #[test] | ||
432 | fn test_wrap_return_type_handles_type_aliases() { | ||
433 | check_fix( | ||
434 | r#" | ||
435 | //- /main.rs | ||
436 | use core::result::Result::{self, Ok, Err}; | ||
437 | |||
438 | type MyResult<T> = Result<T, ()>; | ||
439 | |||
440 | fn div(x: i32, y: i32) -> MyResult<i32> { | ||
441 | if y == 0 { | ||
442 | return Err(()); | ||
443 | } | ||
444 | x <|>/ y | ||
445 | } | ||
446 | //- /core/lib.rs | ||
447 | pub mod result { | ||
448 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
449 | } | ||
450 | "#, | ||
451 | r#" | ||
452 | use core::result::Result::{self, Ok, Err}; | ||
453 | |||
454 | type MyResult<T> = Result<T, ()>; | ||
455 | |||
456 | fn div(x: i32, y: i32) -> MyResult<i32> { | ||
457 | if y == 0 { | ||
458 | return Err(()); | ||
459 | } | ||
460 | Ok(x / y) | ||
461 | } | ||
462 | "#, | ||
463 | ); | ||
464 | } | ||
465 | |||
466 | #[test] | ||
467 | fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() { | ||
468 | check_no_diagnostics( | ||
469 | r#" | ||
470 | //- /main.rs | ||
471 | use core::result::Result::{self, Ok, Err}; | ||
472 | |||
473 | fn foo() -> Result<(), i32> { 0 } | ||
474 | |||
475 | //- /core/lib.rs | ||
476 | pub mod result { | ||
477 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
478 | } | ||
479 | "#, | ||
480 | ); | ||
481 | } | ||
482 | |||
483 | #[test] | ||
484 | fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() { | ||
485 | check_no_diagnostics( | ||
486 | r#" | ||
487 | //- /main.rs | ||
488 | use core::result::Result::{self, Ok, Err}; | ||
489 | |||
490 | enum SomeOtherEnum { Ok(i32), Err(String) } | ||
491 | |||
492 | fn foo() -> SomeOtherEnum { 0 } | ||
493 | |||
494 | //- /core/lib.rs | ||
495 | pub mod result { | ||
496 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
497 | } | ||
498 | "#, | ||
499 | ); | ||
500 | } | ||
501 | |||
502 | #[test] | ||
503 | fn test_fill_struct_fields_empty() { | ||
504 | check_fix( | ||
505 | r#" | ||
506 | struct TestStruct { one: i32, two: i64 } | ||
507 | |||
508 | fn test_fn() { | ||
509 | let s = TestStruct {<|>}; | ||
510 | } | ||
511 | "#, | ||
512 | r#" | ||
513 | struct TestStruct { one: i32, two: i64 } | ||
514 | |||
515 | fn test_fn() { | ||
516 | let s = TestStruct { one: (), two: ()}; | ||
517 | } | ||
518 | "#, | ||
519 | ); | ||
520 | } | ||
521 | |||
522 | #[test] | ||
523 | fn test_fill_struct_fields_self() { | ||
524 | check_fix( | ||
525 | r#" | ||
526 | struct TestStruct { one: i32 } | ||
527 | |||
528 | impl TestStruct { | ||
529 | fn test_fn() { let s = Self {<|>}; } | ||
530 | } | ||
531 | "#, | ||
532 | r#" | ||
533 | struct TestStruct { one: i32 } | ||
534 | |||
535 | impl TestStruct { | ||
536 | fn test_fn() { let s = Self { one: ()}; } | ||
537 | } | ||
538 | "#, | ||
539 | ); | ||
540 | } | ||
541 | |||
542 | #[test] | ||
543 | fn test_fill_struct_fields_enum() { | ||
544 | check_fix( | ||
545 | r#" | ||
546 | enum Expr { | ||
547 | Bin { lhs: Box<Expr>, rhs: Box<Expr> } | ||
548 | } | ||
549 | |||
550 | impl Expr { | ||
551 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | ||
552 | Expr::Bin {<|> } | ||
553 | } | ||
554 | } | ||
555 | "#, | ||
556 | r#" | ||
557 | enum Expr { | ||
558 | Bin { lhs: Box<Expr>, rhs: Box<Expr> } | ||
559 | } | ||
560 | |||
561 | impl Expr { | ||
562 | fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { | ||
563 | Expr::Bin { lhs: (), rhs: () } | ||
564 | } | ||
565 | } | ||
566 | "#, | ||
567 | ); | ||
568 | } | ||
569 | |||
570 | #[test] | ||
571 | fn test_fill_struct_fields_partial() { | ||
572 | check_fix( | ||
573 | r#" | ||
574 | struct TestStruct { one: i32, two: i64 } | ||
575 | |||
576 | fn test_fn() { | ||
577 | let s = TestStruct{ two: 2<|> }; | ||
578 | } | ||
579 | "#, | ||
580 | r" | ||
581 | struct TestStruct { one: i32, two: i64 } | ||
582 | |||
583 | fn test_fn() { | ||
584 | let s = TestStruct{ two: 2, one: () }; | ||
585 | } | ||
586 | ", | ||
587 | ); | ||
588 | } | ||
589 | |||
590 | #[test] | ||
591 | fn test_fill_struct_fields_no_diagnostic() { | ||
592 | check_no_diagnostics( | ||
593 | r" | ||
594 | struct TestStruct { one: i32, two: i64 } | ||
595 | |||
596 | fn test_fn() { | ||
597 | let one = 1; | ||
598 | let s = TestStruct{ one, two: 2 }; | ||
599 | } | ||
600 | ", | ||
601 | ); | ||
602 | } | ||
603 | |||
604 | #[test] | ||
605 | fn test_fill_struct_fields_no_diagnostic_on_spread() { | ||
606 | check_no_diagnostics( | ||
607 | r" | ||
608 | struct TestStruct { one: i32, two: i64 } | ||
609 | |||
610 | fn test_fn() { | ||
611 | let one = 1; | ||
612 | let s = TestStruct{ ..a }; | ||
613 | } | ||
614 | ", | ||
615 | ); | ||
616 | } | ||
617 | |||
618 | #[test] | ||
619 | fn test_unresolved_module_diagnostic() { | ||
620 | check_expect( | ||
621 | r#"mod foo;"#, | ||
622 | expect![[r#" | ||
623 | [ | ||
624 | Diagnostic { | ||
625 | message: "unresolved module", | ||
626 | range: 0..8, | ||
627 | severity: Error, | ||
628 | fix: Some( | ||
629 | Fix { | ||
630 | label: "Create module", | ||
631 | source_change: SourceChange { | ||
632 | source_file_edits: [], | ||
633 | file_system_edits: [ | ||
634 | CreateFile { | ||
635 | anchor: FileId( | ||
636 | 1, | ||
637 | ), | ||
638 | dst: "foo.rs", | ||
639 | }, | ||
640 | ], | ||
641 | is_snippet: false, | ||
642 | }, | ||
643 | }, | ||
644 | ), | ||
645 | }, | ||
646 | ] | ||
647 | "#]], | ||
648 | ); | ||
649 | } | ||
650 | |||
651 | #[test] | ||
652 | fn range_mapping_out_of_macros() { | ||
653 | // FIXME: this is very wrong, but somewhat tricky to fix. | ||
654 | check_fix( | ||
655 | r#" | ||
656 | fn some() {} | ||
657 | fn items() {} | ||
658 | fn here() {} | ||
659 | |||
660 | macro_rules! id { ($($tt:tt)*) => { $($tt)*}; } | ||
661 | |||
662 | fn main() { | ||
663 | let _x = id![Foo { a: <|>42 }]; | ||
664 | } | ||
665 | |||
666 | pub struct Foo { pub a: i32, pub b: i32 } | ||
667 | "#, | ||
668 | r#" | ||
669 | fn {a:42, b: ()} {} | ||
670 | fn items() {} | ||
671 | fn here() {} | ||
672 | |||
673 | macro_rules! id { ($($tt:tt)*) => { $($tt)*}; } | ||
674 | |||
675 | fn main() { | ||
676 | let _x = id![Foo { a: 42 }]; | ||
677 | } | ||
678 | |||
679 | pub struct Foo { pub a: i32, pub b: i32 } | ||
680 | "#, | ||
681 | ); | ||
682 | } | ||
683 | |||
684 | #[test] | ||
685 | fn test_check_unnecessary_braces_in_use_statement() { | ||
686 | check_no_diagnostics( | ||
687 | r#" | ||
688 | use a; | ||
689 | use a::{c, d::e}; | ||
690 | "#, | ||
691 | ); | ||
692 | check_fix(r#"use {<|>b};"#, r#"use b;"#); | ||
693 | check_fix(r#"use {b<|>};"#, r#"use b;"#); | ||
694 | check_fix(r#"use a::{c<|>};"#, r#"use a::c;"#); | ||
695 | check_fix(r#"use a::{self<|>};"#, r#"use a;"#); | ||
696 | check_fix(r#"use a::{c, d::{e<|>}};"#, r#"use a::{c, d::e};"#); | ||
697 | } | ||
698 | |||
699 | #[test] | ||
700 | fn test_check_struct_shorthand_initialization() { | ||
701 | check_no_diagnostics( | ||
702 | r#" | ||
703 | struct A { a: &'static str } | ||
704 | fn main() { A { a: "hello" } } | ||
705 | "#, | ||
706 | ); | ||
707 | check_no_diagnostics( | ||
708 | r#" | ||
709 | struct A(usize); | ||
710 | fn main() { A { 0: 0 } } | ||
711 | "#, | ||
712 | ); | ||
713 | |||
714 | check_fix( | ||
715 | r#" | ||
716 | struct A { a: &'static str } | ||
717 | fn main() { | ||
718 | let a = "haha"; | ||
719 | A { a<|>: a } | ||
720 | } | ||
721 | "#, | ||
722 | r#" | ||
723 | struct A { a: &'static str } | ||
724 | fn main() { | ||
725 | let a = "haha"; | ||
726 | A { a } | ||
727 | } | ||
728 | "#, | ||
729 | ); | ||
730 | |||
731 | check_fix( | ||
732 | r#" | ||
733 | struct A { a: &'static str, b: &'static str } | ||
734 | fn main() { | ||
735 | let a = "haha"; | ||
736 | let b = "bb"; | ||
737 | A { a<|>: a, b } | ||
738 | } | ||
739 | "#, | ||
740 | r#" | ||
741 | struct A { a: &'static str, b: &'static str } | ||
742 | fn main() { | ||
743 | let a = "haha"; | ||
744 | let b = "bb"; | ||
745 | A { a, b } | ||
746 | } | ||
747 | "#, | ||
748 | ); | ||
749 | } | ||
750 | |||
751 | #[test] | ||
752 | fn test_add_field_from_usage() { | ||
753 | check_fix( | ||
754 | r" | ||
755 | fn main() { | ||
756 | Foo { bar: 3, baz<|>: false}; | ||
757 | } | ||
758 | struct Foo { | ||
759 | bar: i32 | ||
760 | } | ||
761 | ", | ||
762 | r" | ||
763 | fn main() { | ||
764 | Foo { bar: 3, baz: false}; | ||
765 | } | ||
766 | struct Foo { | ||
767 | bar: i32, | ||
768 | baz: bool | ||
769 | } | ||
770 | ", | ||
771 | ) | ||
772 | } | ||
773 | |||
774 | #[test] | ||
775 | fn test_add_field_in_other_file_from_usage() { | ||
776 | check_apply_diagnostic_fix_in_other_file( | ||
777 | r" | ||
778 | //- /main.rs | ||
779 | mod foo; | ||
780 | |||
781 | fn main() { | ||
782 | <|>foo::Foo { bar: 3, baz: false}; | ||
783 | } | ||
784 | //- /foo.rs | ||
785 | struct Foo { | ||
786 | bar: i32 | ||
787 | } | ||
788 | ", | ||
789 | r" | ||
790 | struct Foo { | ||
791 | bar: i32, | ||
792 | pub(crate) baz: bool | ||
793 | } | ||
794 | ", | ||
795 | ) | ||
796 | } | ||
797 | } | ||