diff options
Diffstat (limited to 'crates/hir_ty/src/diagnostics.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics.rs | 769 |
1 files changed, 14 insertions, 755 deletions
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index 283894704..407273943 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -4,325 +4,31 @@ mod match_check; | |||
4 | mod unsafe_check; | 4 | mod unsafe_check; |
5 | mod decl_check; | 5 | mod decl_check; |
6 | 6 | ||
7 | use std::{any::Any, fmt}; | 7 | use std::fmt; |
8 | 8 | ||
9 | use base_db::CrateId; | 9 | use base_db::CrateId; |
10 | use hir_def::{DefWithBodyId, ModuleDefId}; | 10 | use hir_def::ModuleDefId; |
11 | use hir_expand::{name::Name, HirFileId, InFile}; | 11 | use hir_expand::HirFileId; |
12 | use stdx::format_to; | 12 | use syntax::{ast, AstPtr}; |
13 | use syntax::{ast, AstPtr, SyntaxNodePtr}; | ||
14 | 13 | ||
15 | use crate::{ | 14 | use crate::db::HirDatabase; |
16 | db::HirDatabase, | ||
17 | diagnostics_sink::{Diagnostic, DiagnosticCode, DiagnosticSink}, | ||
18 | }; | ||
19 | 15 | ||
20 | pub use crate::diagnostics::expr::{record_literal_missing_fields, record_pattern_missing_fields}; | 16 | pub use crate::diagnostics::{ |
17 | expr::{ | ||
18 | record_literal_missing_fields, record_pattern_missing_fields, BodyValidationDiagnostic, | ||
19 | }, | ||
20 | unsafe_check::missing_unsafe, | ||
21 | }; | ||
21 | 22 | ||
22 | pub fn validate_module_item( | 23 | pub fn validate_module_item( |
23 | db: &dyn HirDatabase, | 24 | db: &dyn HirDatabase, |
24 | krate: CrateId, | 25 | krate: CrateId, |
25 | owner: ModuleDefId, | 26 | owner: ModuleDefId, |
26 | sink: &mut DiagnosticSink<'_>, | 27 | ) -> Vec<IncorrectCase> { |
27 | ) { | ||
28 | let _p = profile::span("validate_module_item"); | 28 | let _p = profile::span("validate_module_item"); |
29 | let mut validator = decl_check::DeclValidator::new(db, krate, sink); | 29 | let mut validator = decl_check::DeclValidator::new(db, krate); |
30 | validator.validate_item(owner); | 30 | validator.validate_item(owner); |
31 | } | 31 | validator.sink |
32 | |||
33 | pub fn validate_body(db: &dyn HirDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) { | ||
34 | let _p = profile::span("validate_body"); | ||
35 | let infer = db.infer(owner); | ||
36 | infer.add_diagnostics(db, owner, sink); | ||
37 | let mut validator = expr::ExprValidator::new(owner, infer.clone(), sink); | ||
38 | validator.validate_body(db); | ||
39 | let mut validator = unsafe_check::UnsafeValidator::new(owner, infer, sink); | ||
40 | validator.validate_body(db); | ||
41 | } | ||
42 | |||
43 | // Diagnostic: no-such-field | ||
44 | // | ||
45 | // This diagnostic is triggered if created structure does not have field provided in record. | ||
46 | #[derive(Debug)] | ||
47 | pub struct NoSuchField { | ||
48 | pub file: HirFileId, | ||
49 | pub field: AstPtr<ast::RecordExprField>, | ||
50 | } | ||
51 | |||
52 | impl Diagnostic for NoSuchField { | ||
53 | fn code(&self) -> DiagnosticCode { | ||
54 | DiagnosticCode("no-such-field") | ||
55 | } | ||
56 | |||
57 | fn message(&self) -> String { | ||
58 | "no such field".to_string() | ||
59 | } | ||
60 | |||
61 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
62 | InFile::new(self.file, self.field.clone().into()) | ||
63 | } | ||
64 | |||
65 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
66 | self | ||
67 | } | ||
68 | } | ||
69 | |||
70 | // Diagnostic: missing-structure-fields | ||
71 | // | ||
72 | // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. | ||
73 | // | ||
74 | // Example: | ||
75 | // | ||
76 | // ```rust | ||
77 | // struct A { a: u8, b: u8 } | ||
78 | // | ||
79 | // let a = A { a: 10 }; | ||
80 | // ``` | ||
81 | #[derive(Debug)] | ||
82 | pub struct MissingFields { | ||
83 | pub file: HirFileId, | ||
84 | pub field_list_parent: AstPtr<ast::RecordExpr>, | ||
85 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, | ||
86 | pub missed_fields: Vec<Name>, | ||
87 | } | ||
88 | |||
89 | impl Diagnostic for MissingFields { | ||
90 | fn code(&self) -> DiagnosticCode { | ||
91 | DiagnosticCode("missing-structure-fields") | ||
92 | } | ||
93 | fn message(&self) -> String { | ||
94 | let mut buf = String::from("Missing structure fields:\n"); | ||
95 | for field in &self.missed_fields { | ||
96 | format_to!(buf, "- {}\n", field); | ||
97 | } | ||
98 | buf | ||
99 | } | ||
100 | |||
101 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
102 | InFile { | ||
103 | file_id: self.file, | ||
104 | value: self | ||
105 | .field_list_parent_path | ||
106 | .clone() | ||
107 | .map(SyntaxNodePtr::from) | ||
108 | .unwrap_or_else(|| self.field_list_parent.clone().into()), | ||
109 | } | ||
110 | } | ||
111 | |||
112 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
113 | self | ||
114 | } | ||
115 | } | ||
116 | |||
117 | // Diagnostic: missing-pat-fields | ||
118 | // | ||
119 | // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. | ||
120 | // | ||
121 | // Example: | ||
122 | // | ||
123 | // ```rust | ||
124 | // struct A { a: u8, b: u8 } | ||
125 | // | ||
126 | // let a = A { a: 10, b: 20 }; | ||
127 | // | ||
128 | // if let A { a } = a { | ||
129 | // // ... | ||
130 | // } | ||
131 | // ``` | ||
132 | #[derive(Debug)] | ||
133 | pub struct MissingPatFields { | ||
134 | pub file: HirFileId, | ||
135 | pub field_list_parent: AstPtr<ast::RecordPat>, | ||
136 | pub field_list_parent_path: Option<AstPtr<ast::Path>>, | ||
137 | pub missed_fields: Vec<Name>, | ||
138 | } | ||
139 | |||
140 | impl Diagnostic for MissingPatFields { | ||
141 | fn code(&self) -> DiagnosticCode { | ||
142 | DiagnosticCode("missing-pat-fields") | ||
143 | } | ||
144 | fn message(&self) -> String { | ||
145 | let mut buf = String::from("Missing structure fields:\n"); | ||
146 | for field in &self.missed_fields { | ||
147 | format_to!(buf, "- {}\n", field); | ||
148 | } | ||
149 | buf | ||
150 | } | ||
151 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
152 | InFile { | ||
153 | file_id: self.file, | ||
154 | value: self | ||
155 | .field_list_parent_path | ||
156 | .clone() | ||
157 | .map(SyntaxNodePtr::from) | ||
158 | .unwrap_or_else(|| self.field_list_parent.clone().into()), | ||
159 | } | ||
160 | } | ||
161 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
162 | self | ||
163 | } | ||
164 | } | ||
165 | |||
166 | // Diagnostic: missing-match-arm | ||
167 | // | ||
168 | // This diagnostic is triggered if `match` block is missing one or more match arms. | ||
169 | #[derive(Debug)] | ||
170 | pub struct MissingMatchArms { | ||
171 | pub file: HirFileId, | ||
172 | pub match_expr: AstPtr<ast::Expr>, | ||
173 | pub arms: AstPtr<ast::MatchArmList>, | ||
174 | } | ||
175 | |||
176 | impl Diagnostic for MissingMatchArms { | ||
177 | fn code(&self) -> DiagnosticCode { | ||
178 | DiagnosticCode("missing-match-arm") | ||
179 | } | ||
180 | fn message(&self) -> String { | ||
181 | String::from("Missing match arm") | ||
182 | } | ||
183 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
184 | InFile { file_id: self.file, value: self.match_expr.clone().into() } | ||
185 | } | ||
186 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
187 | self | ||
188 | } | ||
189 | } | ||
190 | |||
191 | // Diagnostic: missing-ok-or-some-in-tail-expr | ||
192 | // | ||
193 | // This diagnostic is triggered if a block that should return `Result` returns a value not wrapped in `Ok`, | ||
194 | // or if a block that should return `Option` returns a value not wrapped in `Some`. | ||
195 | // | ||
196 | // Example: | ||
197 | // | ||
198 | // ```rust | ||
199 | // fn foo() -> Result<u8, ()> { | ||
200 | // 10 | ||
201 | // } | ||
202 | // ``` | ||
203 | #[derive(Debug)] | ||
204 | pub struct MissingOkOrSomeInTailExpr { | ||
205 | pub file: HirFileId, | ||
206 | pub expr: AstPtr<ast::Expr>, | ||
207 | // `Some` or `Ok` depending on whether the return type is Result or Option | ||
208 | pub required: String, | ||
209 | } | ||
210 | |||
211 | impl Diagnostic for MissingOkOrSomeInTailExpr { | ||
212 | fn code(&self) -> DiagnosticCode { | ||
213 | DiagnosticCode("missing-ok-or-some-in-tail-expr") | ||
214 | } | ||
215 | fn message(&self) -> String { | ||
216 | format!("wrap return expression in {}", self.required) | ||
217 | } | ||
218 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
219 | InFile { file_id: self.file, value: self.expr.clone().into() } | ||
220 | } | ||
221 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
222 | self | ||
223 | } | ||
224 | } | ||
225 | |||
226 | #[derive(Debug)] | ||
227 | pub struct RemoveThisSemicolon { | ||
228 | pub file: HirFileId, | ||
229 | pub expr: AstPtr<ast::Expr>, | ||
230 | } | ||
231 | |||
232 | impl Diagnostic for RemoveThisSemicolon { | ||
233 | fn code(&self) -> DiagnosticCode { | ||
234 | DiagnosticCode("remove-this-semicolon") | ||
235 | } | ||
236 | |||
237 | fn message(&self) -> String { | ||
238 | "Remove this semicolon".to_string() | ||
239 | } | ||
240 | |||
241 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
242 | InFile { file_id: self.file, value: self.expr.clone().into() } | ||
243 | } | ||
244 | |||
245 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
246 | self | ||
247 | } | ||
248 | } | ||
249 | |||
250 | // Diagnostic: break-outside-of-loop | ||
251 | // | ||
252 | // This diagnostic is triggered if the `break` keyword is used outside of a loop. | ||
253 | #[derive(Debug)] | ||
254 | pub struct BreakOutsideOfLoop { | ||
255 | pub file: HirFileId, | ||
256 | pub expr: AstPtr<ast::Expr>, | ||
257 | } | ||
258 | |||
259 | impl Diagnostic for BreakOutsideOfLoop { | ||
260 | fn code(&self) -> DiagnosticCode { | ||
261 | DiagnosticCode("break-outside-of-loop") | ||
262 | } | ||
263 | fn message(&self) -> String { | ||
264 | "break outside of loop".to_string() | ||
265 | } | ||
266 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
267 | InFile { file_id: self.file, value: self.expr.clone().into() } | ||
268 | } | ||
269 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
270 | self | ||
271 | } | ||
272 | } | ||
273 | |||
274 | // Diagnostic: missing-unsafe | ||
275 | // | ||
276 | // This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block. | ||
277 | #[derive(Debug)] | ||
278 | pub struct MissingUnsafe { | ||
279 | pub file: HirFileId, | ||
280 | pub expr: AstPtr<ast::Expr>, | ||
281 | } | ||
282 | |||
283 | impl Diagnostic for MissingUnsafe { | ||
284 | fn code(&self) -> DiagnosticCode { | ||
285 | DiagnosticCode("missing-unsafe") | ||
286 | } | ||
287 | fn message(&self) -> String { | ||
288 | format!("This operation is unsafe and requires an unsafe function or block") | ||
289 | } | ||
290 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
291 | InFile { file_id: self.file, value: self.expr.clone().into() } | ||
292 | } | ||
293 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
294 | self | ||
295 | } | ||
296 | } | ||
297 | |||
298 | // Diagnostic: mismatched-arg-count | ||
299 | // | ||
300 | // This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. | ||
301 | #[derive(Debug)] | ||
302 | pub struct MismatchedArgCount { | ||
303 | pub file: HirFileId, | ||
304 | pub call_expr: AstPtr<ast::Expr>, | ||
305 | pub expected: usize, | ||
306 | pub found: usize, | ||
307 | } | ||
308 | |||
309 | impl Diagnostic for MismatchedArgCount { | ||
310 | fn code(&self) -> DiagnosticCode { | ||
311 | DiagnosticCode("mismatched-arg-count") | ||
312 | } | ||
313 | fn message(&self) -> String { | ||
314 | let s = if self.expected == 1 { "" } else { "s" }; | ||
315 | format!("Expected {} argument{}, found {}", self.expected, s, self.found) | ||
316 | } | ||
317 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
318 | InFile { file_id: self.file, value: self.call_expr.clone().into() } | ||
319 | } | ||
320 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
321 | self | ||
322 | } | ||
323 | fn is_experimental(&self) -> bool { | ||
324 | true | ||
325 | } | ||
326 | } | 32 | } |
327 | 33 | ||
328 | #[derive(Debug)] | 34 | #[derive(Debug)] |
@@ -390,450 +96,3 @@ pub struct IncorrectCase { | |||
390 | pub ident_text: String, | 96 | pub ident_text: String, |
391 | pub suggested_text: String, | 97 | pub suggested_text: String, |
392 | } | 98 | } |
393 | |||
394 | impl Diagnostic for IncorrectCase { | ||
395 | fn code(&self) -> DiagnosticCode { | ||
396 | DiagnosticCode("incorrect-ident-case") | ||
397 | } | ||
398 | |||
399 | fn message(&self) -> String { | ||
400 | format!( | ||
401 | "{} `{}` should have {} name, e.g. `{}`", | ||
402 | self.ident_type, | ||
403 | self.ident_text, | ||
404 | self.expected_case.to_string(), | ||
405 | self.suggested_text | ||
406 | ) | ||
407 | } | ||
408 | |||
409 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
410 | InFile::new(self.file, self.ident.clone().into()) | ||
411 | } | ||
412 | |||
413 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
414 | self | ||
415 | } | ||
416 | |||
417 | fn is_experimental(&self) -> bool { | ||
418 | true | ||
419 | } | ||
420 | } | ||
421 | |||
422 | // Diagnostic: replace-filter-map-next-with-find-map | ||
423 | // | ||
424 | // This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. | ||
425 | #[derive(Debug)] | ||
426 | pub struct ReplaceFilterMapNextWithFindMap { | ||
427 | pub file: HirFileId, | ||
428 | /// This expression is the whole method chain up to and including `.filter_map(..).next()`. | ||
429 | pub next_expr: AstPtr<ast::Expr>, | ||
430 | } | ||
431 | |||
432 | impl Diagnostic for ReplaceFilterMapNextWithFindMap { | ||
433 | fn code(&self) -> DiagnosticCode { | ||
434 | DiagnosticCode("replace-filter-map-next-with-find-map") | ||
435 | } | ||
436 | fn message(&self) -> String { | ||
437 | "replace filter_map(..).next() with find_map(..)".to_string() | ||
438 | } | ||
439 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
440 | InFile { file_id: self.file, value: self.next_expr.clone().into() } | ||
441 | } | ||
442 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
443 | self | ||
444 | } | ||
445 | } | ||
446 | |||
447 | #[cfg(test)] | ||
448 | mod tests { | ||
449 | use base_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; | ||
450 | use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId}; | ||
451 | use hir_expand::db::AstDatabase; | ||
452 | use rustc_hash::FxHashMap; | ||
453 | use syntax::{TextRange, TextSize}; | ||
454 | |||
455 | use crate::{ | ||
456 | diagnostics::{validate_body, validate_module_item}, | ||
457 | diagnostics_sink::{Diagnostic, DiagnosticSinkBuilder}, | ||
458 | test_db::TestDB, | ||
459 | }; | ||
460 | |||
461 | impl TestDB { | ||
462 | fn diagnostics<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) { | ||
463 | let crate_graph = self.crate_graph(); | ||
464 | for krate in crate_graph.iter() { | ||
465 | let crate_def_map = self.crate_def_map(krate); | ||
466 | |||
467 | let mut fns = Vec::new(); | ||
468 | for (module_id, _) in crate_def_map.modules() { | ||
469 | for decl in crate_def_map[module_id].scope.declarations() { | ||
470 | let mut sink = DiagnosticSinkBuilder::new().build(&mut cb); | ||
471 | validate_module_item(self, krate, decl, &mut sink); | ||
472 | |||
473 | if let ModuleDefId::FunctionId(f) = decl { | ||
474 | fns.push(f) | ||
475 | } | ||
476 | } | ||
477 | |||
478 | for impl_id in crate_def_map[module_id].scope.impls() { | ||
479 | let impl_data = self.impl_data(impl_id); | ||
480 | for item in impl_data.items.iter() { | ||
481 | if let AssocItemId::FunctionId(f) = item { | ||
482 | let mut sink = DiagnosticSinkBuilder::new().build(&mut cb); | ||
483 | validate_module_item( | ||
484 | self, | ||
485 | krate, | ||
486 | ModuleDefId::FunctionId(*f), | ||
487 | &mut sink, | ||
488 | ); | ||
489 | fns.push(*f) | ||
490 | } | ||
491 | } | ||
492 | } | ||
493 | } | ||
494 | |||
495 | for f in fns { | ||
496 | let mut sink = DiagnosticSinkBuilder::new().build(&mut cb); | ||
497 | validate_body(self, f.into(), &mut sink); | ||
498 | } | ||
499 | } | ||
500 | } | ||
501 | } | ||
502 | |||
503 | pub(crate) fn check_diagnostics(ra_fixture: &str) { | ||
504 | let db = TestDB::with_files(ra_fixture); | ||
505 | let annotations = db.extract_annotations(); | ||
506 | |||
507 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); | ||
508 | db.diagnostics(|d| { | ||
509 | let src = d.display_source(); | ||
510 | let root = db.parse_or_expand(src.file_id).unwrap(); | ||
511 | // FIXME: macros... | ||
512 | let file_id = src.file_id.original_file(&db); | ||
513 | let range = src.value.to_node(&root).text_range(); | ||
514 | let message = d.message(); | ||
515 | actual.entry(file_id).or_default().push((range, message)); | ||
516 | }); | ||
517 | |||
518 | for (file_id, diags) in actual.iter_mut() { | ||
519 | diags.sort_by_key(|it| it.0.start()); | ||
520 | let text = db.file_text(*file_id); | ||
521 | // For multiline spans, place them on line start | ||
522 | for (range, content) in diags { | ||
523 | if text[*range].contains('\n') { | ||
524 | *range = TextRange::new(range.start(), range.start() + TextSize::from(1)); | ||
525 | *content = format!("... {}", content); | ||
526 | } | ||
527 | } | ||
528 | } | ||
529 | |||
530 | assert_eq!(annotations, actual); | ||
531 | } | ||
532 | |||
533 | #[test] | ||
534 | fn no_such_field_diagnostics() { | ||
535 | check_diagnostics( | ||
536 | r#" | ||
537 | struct S { foo: i32, bar: () } | ||
538 | impl S { | ||
539 | fn new() -> S { | ||
540 | S { | ||
541 | //^ Missing structure fields: | ||
542 | //| - bar | ||
543 | foo: 92, | ||
544 | baz: 62, | ||
545 | //^^^^^^^ no such field | ||
546 | } | ||
547 | } | ||
548 | } | ||
549 | "#, | ||
550 | ); | ||
551 | } | ||
552 | #[test] | ||
553 | fn no_such_field_with_feature_flag_diagnostics() { | ||
554 | check_diagnostics( | ||
555 | r#" | ||
556 | //- /lib.rs crate:foo cfg:feature=foo | ||
557 | struct MyStruct { | ||
558 | my_val: usize, | ||
559 | #[cfg(feature = "foo")] | ||
560 | bar: bool, | ||
561 | } | ||
562 | |||
563 | impl MyStruct { | ||
564 | #[cfg(feature = "foo")] | ||
565 | pub(crate) fn new(my_val: usize, bar: bool) -> Self { | ||
566 | Self { my_val, bar } | ||
567 | } | ||
568 | #[cfg(not(feature = "foo"))] | ||
569 | pub(crate) fn new(my_val: usize, _bar: bool) -> Self { | ||
570 | Self { my_val } | ||
571 | } | ||
572 | } | ||
573 | "#, | ||
574 | ); | ||
575 | } | ||
576 | |||
577 | #[test] | ||
578 | fn no_such_field_enum_with_feature_flag_diagnostics() { | ||
579 | check_diagnostics( | ||
580 | r#" | ||
581 | //- /lib.rs crate:foo cfg:feature=foo | ||
582 | enum Foo { | ||
583 | #[cfg(not(feature = "foo"))] | ||
584 | Buz, | ||
585 | #[cfg(feature = "foo")] | ||
586 | Bar, | ||
587 | Baz | ||
588 | } | ||
589 | |||
590 | fn test_fn(f: Foo) { | ||
591 | match f { | ||
592 | Foo::Bar => {}, | ||
593 | Foo::Baz => {}, | ||
594 | } | ||
595 | } | ||
596 | "#, | ||
597 | ); | ||
598 | } | ||
599 | |||
600 | #[test] | ||
601 | fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() { | ||
602 | check_diagnostics( | ||
603 | r#" | ||
604 | //- /lib.rs crate:foo cfg:feature=foo | ||
605 | struct S { | ||
606 | #[cfg(feature = "foo")] | ||
607 | foo: u32, | ||
608 | #[cfg(not(feature = "foo"))] | ||
609 | bar: u32, | ||
610 | } | ||
611 | |||
612 | impl S { | ||
613 | #[cfg(feature = "foo")] | ||
614 | fn new(foo: u32) -> Self { | ||
615 | Self { foo } | ||
616 | } | ||
617 | #[cfg(not(feature = "foo"))] | ||
618 | fn new(bar: u32) -> Self { | ||
619 | Self { bar } | ||
620 | } | ||
621 | fn new2(bar: u32) -> Self { | ||
622 | #[cfg(feature = "foo")] | ||
623 | { Self { foo: bar } } | ||
624 | #[cfg(not(feature = "foo"))] | ||
625 | { Self { bar } } | ||
626 | } | ||
627 | fn new2(val: u32) -> Self { | ||
628 | Self { | ||
629 | #[cfg(feature = "foo")] | ||
630 | foo: val, | ||
631 | #[cfg(not(feature = "foo"))] | ||
632 | bar: val, | ||
633 | } | ||
634 | } | ||
635 | } | ||
636 | "#, | ||
637 | ); | ||
638 | } | ||
639 | |||
640 | #[test] | ||
641 | fn no_such_field_with_type_macro() { | ||
642 | check_diagnostics( | ||
643 | r#" | ||
644 | macro_rules! Type { () => { u32 }; } | ||
645 | struct Foo { bar: Type![] } | ||
646 | |||
647 | impl Foo { | ||
648 | fn new() -> Self { | ||
649 | Foo { bar: 0 } | ||
650 | } | ||
651 | } | ||
652 | "#, | ||
653 | ); | ||
654 | } | ||
655 | |||
656 | #[test] | ||
657 | fn missing_record_pat_field_diagnostic() { | ||
658 | check_diagnostics( | ||
659 | r#" | ||
660 | struct S { foo: i32, bar: () } | ||
661 | fn baz(s: S) { | ||
662 | let S { foo: _ } = s; | ||
663 | //^ Missing structure fields: | ||
664 | //| - bar | ||
665 | } | ||
666 | "#, | ||
667 | ); | ||
668 | } | ||
669 | |||
670 | #[test] | ||
671 | fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() { | ||
672 | check_diagnostics( | ||
673 | r" | ||
674 | struct S { foo: i32, bar: () } | ||
675 | fn baz(s: S) -> i32 { | ||
676 | match s { | ||
677 | S { foo, .. } => foo, | ||
678 | } | ||
679 | } | ||
680 | ", | ||
681 | ) | ||
682 | } | ||
683 | |||
684 | #[test] | ||
685 | fn missing_record_pat_field_box() { | ||
686 | check_diagnostics( | ||
687 | r" | ||
688 | struct S { s: Box<u32> } | ||
689 | fn x(a: S) { | ||
690 | let S { box s } = a; | ||
691 | } | ||
692 | ", | ||
693 | ) | ||
694 | } | ||
695 | |||
696 | #[test] | ||
697 | fn missing_record_pat_field_ref() { | ||
698 | check_diagnostics( | ||
699 | r" | ||
700 | struct S { s: u32 } | ||
701 | fn x(a: S) { | ||
702 | let S { ref s } = a; | ||
703 | } | ||
704 | ", | ||
705 | ) | ||
706 | } | ||
707 | |||
708 | #[test] | ||
709 | fn import_extern_crate_clash_with_inner_item() { | ||
710 | // This is more of a resolver test, but doesn't really work with the hir_def testsuite. | ||
711 | |||
712 | check_diagnostics( | ||
713 | r#" | ||
714 | //- /lib.rs crate:lib deps:jwt | ||
715 | mod permissions; | ||
716 | |||
717 | use permissions::jwt; | ||
718 | |||
719 | fn f() { | ||
720 | fn inner() {} | ||
721 | jwt::Claims {}; // should resolve to the local one with 0 fields, and not get a diagnostic | ||
722 | } | ||
723 | |||
724 | //- /permissions.rs | ||
725 | pub mod jwt { | ||
726 | pub struct Claims {} | ||
727 | } | ||
728 | |||
729 | //- /jwt/lib.rs crate:jwt | ||
730 | pub struct Claims { | ||
731 | field: u8, | ||
732 | } | ||
733 | "#, | ||
734 | ); | ||
735 | } | ||
736 | |||
737 | #[test] | ||
738 | fn break_outside_of_loop() { | ||
739 | check_diagnostics( | ||
740 | r#" | ||
741 | fn foo() { break; } | ||
742 | //^^^^^ break outside of loop | ||
743 | "#, | ||
744 | ); | ||
745 | } | ||
746 | |||
747 | #[test] | ||
748 | fn missing_semicolon() { | ||
749 | check_diagnostics( | ||
750 | r#" | ||
751 | fn test() -> i32 { 123; } | ||
752 | //^^^ Remove this semicolon | ||
753 | "#, | ||
754 | ); | ||
755 | } | ||
756 | |||
757 | // Register the required standard library types to make the tests work | ||
758 | fn add_filter_map_with_find_next_boilerplate(body: &str) -> String { | ||
759 | let prefix = r#" | ||
760 | //- /main.rs crate:main deps:core | ||
761 | use core::iter::Iterator; | ||
762 | use core::option::Option::{self, Some, None}; | ||
763 | "#; | ||
764 | let suffix = r#" | ||
765 | //- /core/lib.rs crate:core | ||
766 | pub mod option { | ||
767 | pub enum Option<T> { Some(T), None } | ||
768 | } | ||
769 | pub mod iter { | ||
770 | pub trait Iterator { | ||
771 | type Item; | ||
772 | fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap } | ||
773 | fn next(&mut self) -> Option<Self::Item>; | ||
774 | } | ||
775 | pub struct FilterMap {} | ||
776 | impl Iterator for FilterMap { | ||
777 | type Item = i32; | ||
778 | fn next(&mut self) -> i32 { 7 } | ||
779 | } | ||
780 | } | ||
781 | "#; | ||
782 | format!("{}{}{}", prefix, body, suffix) | ||
783 | } | ||
784 | |||
785 | #[test] | ||
786 | fn replace_filter_map_next_with_find_map2() { | ||
787 | check_diagnostics(&add_filter_map_with_find_next_boilerplate( | ||
788 | r#" | ||
789 | fn foo() { | ||
790 | let m = [1, 2, 3].iter().filter_map(|x| if *x == 2 { Some (4) } else { None }).next(); | ||
791 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ replace filter_map(..).next() with find_map(..) | ||
792 | } | ||
793 | "#, | ||
794 | )); | ||
795 | } | ||
796 | |||
797 | #[test] | ||
798 | fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() { | ||
799 | check_diagnostics(&add_filter_map_with_find_next_boilerplate( | ||
800 | r#" | ||
801 | fn foo() { | ||
802 | let m = [1, 2, 3] | ||
803 | .iter() | ||
804 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | ||
805 | .len(); | ||
806 | } | ||
807 | "#, | ||
808 | )); | ||
809 | } | ||
810 | |||
811 | #[test] | ||
812 | fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() { | ||
813 | check_diagnostics(&add_filter_map_with_find_next_boilerplate( | ||
814 | r#" | ||
815 | fn foo() { | ||
816 | let m = [1, 2, 3] | ||
817 | .iter() | ||
818 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | ||
819 | .map(|x| x + 2) | ||
820 | .len(); | ||
821 | } | ||
822 | "#, | ||
823 | )); | ||
824 | } | ||
825 | |||
826 | #[test] | ||
827 | fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() { | ||
828 | check_diagnostics(&add_filter_map_with_find_next_boilerplate( | ||
829 | r#" | ||
830 | fn foo() { | ||
831 | let m = [1, 2, 3] | ||
832 | .iter() | ||
833 | .filter_map(|x| if *x == 2 { Some (4) } else { None }); | ||
834 | let n = m.next(); | ||
835 | } | ||
836 | "#, | ||
837 | )); | ||
838 | } | ||
839 | } | ||