diff options
author | Aleksey Kladov <[email protected]> | 2021-06-13 14:05:43 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-13 14:05:43 +0100 |
commit | 39f190b72ccab9a166529f6ae1c8d5d562b15571 (patch) | |
tree | b9035504de80a31b9071da6102a983e7ee260d18 /crates/ide | |
parent | 6383252cc2770545505d40217732f14e93a396c4 (diff) |
internal: refactor unresolved extern crate diagnostic
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/unresolved_extern_crate.rs | 49 |
2 files changed, 52 insertions, 0 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 3307e240b..1fbb7131d 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -5,6 +5,7 @@ | |||
5 | //! original files. So we need to map the ranges. | 5 | //! original files. So we need to map the ranges. |
6 | 6 | ||
7 | mod unresolved_module; | 7 | mod unresolved_module; |
8 | mod unresolved_extern_crate; | ||
8 | mod missing_fields; | 9 | mod missing_fields; |
9 | 10 | ||
10 | mod fixes; | 11 | mod fixes; |
@@ -229,8 +230,10 @@ pub(crate) fn diagnostics( | |||
229 | 230 | ||
230 | let ctx = DiagnosticsContext { config, sema, resolve }; | 231 | let ctx = DiagnosticsContext { config, sema, resolve }; |
231 | for diag in diags { | 232 | for diag in diags { |
233 | #[rustfmt::skip] | ||
232 | let d = match diag { | 234 | let d = match diag { |
233 | AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), | 235 | AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), |
236 | AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), | ||
234 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), | 237 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), |
235 | }; | 238 | }; |
236 | if let Some(code) = d.code { | 239 | if let Some(code) = d.code { |
diff --git a/crates/ide/src/diagnostics/unresolved_extern_crate.rs b/crates/ide/src/diagnostics/unresolved_extern_crate.rs new file mode 100644 index 000000000..2ea79c2ee --- /dev/null +++ b/crates/ide/src/diagnostics/unresolved_extern_crate.rs | |||
@@ -0,0 +1,49 @@ | |||
1 | use crate::diagnostics::{Diagnostic, DiagnosticsContext}; | ||
2 | |||
3 | // Diagnostic: unresolved-extern-crate | ||
4 | // | ||
5 | // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. | ||
6 | pub(super) fn unresolved_extern_crate( | ||
7 | ctx: &DiagnosticsContext<'_>, | ||
8 | d: &hir::UnresolvedExternCrate, | ||
9 | ) -> Diagnostic { | ||
10 | Diagnostic::new( | ||
11 | "unresolved-extern-crate", | ||
12 | "unresolved extern crate", | ||
13 | ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range, | ||
14 | ) | ||
15 | } | ||
16 | |||
17 | #[cfg(test)] | ||
18 | mod tests { | ||
19 | use crate::diagnostics::tests::check_diagnostics; | ||
20 | |||
21 | #[test] | ||
22 | fn unresolved_extern_crate() { | ||
23 | check_diagnostics( | ||
24 | r#" | ||
25 | //- /main.rs crate:main deps:core | ||
26 | extern crate core; | ||
27 | extern crate doesnotexist; | ||
28 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | ||
29 | //- /lib.rs crate:core | ||
30 | "#, | ||
31 | ); | ||
32 | } | ||
33 | |||
34 | #[test] | ||
35 | fn extern_crate_self_as() { | ||
36 | cov_mark::check!(extern_crate_self_as); | ||
37 | check_diagnostics( | ||
38 | r#" | ||
39 | //- /lib.rs | ||
40 | extern crate doesnotexist; | ||
41 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | ||
42 | // Should not error. | ||
43 | extern crate self as foo; | ||
44 | struct Foo; | ||
45 | use foo::Foo as Bar; | ||
46 | "#, | ||
47 | ); | ||
48 | } | ||
49 | } | ||