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