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