From 39f190b72ccab9a166529f6ae1c8d5d562b15571 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 16:05:43 +0300 Subject: internal: refactor unresolved extern crate diagnostic --- crates/ide/src/diagnostics.rs | 3 ++ .../ide/src/diagnostics/unresolved_extern_crate.rs | 49 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 crates/ide/src/diagnostics/unresolved_extern_crate.rs (limited to 'crates/ide/src') 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 @@ //! original files. So we need to map the ranges. mod unresolved_module; +mod unresolved_extern_crate; mod missing_fields; mod fixes; @@ -229,8 +230,10 @@ pub(crate) fn diagnostics( let ctx = DiagnosticsContext { config, sema, resolve }; for diag in diags { + #[rustfmt::skip] let d = match diag { AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), + AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), }; 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 @@ +use crate::diagnostics::{Diagnostic, DiagnosticsContext}; + +// Diagnostic: unresolved-extern-crate +// +// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. +pub(super) fn unresolved_extern_crate( + ctx: &DiagnosticsContext<'_>, + d: &hir::UnresolvedExternCrate, +) -> Diagnostic { + Diagnostic::new( + "unresolved-extern-crate", + "unresolved extern crate", + ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range, + ) +} + +#[cfg(test)] +mod tests { + use crate::diagnostics::tests::check_diagnostics; + + #[test] + fn unresolved_extern_crate() { + check_diagnostics( + r#" +//- /main.rs crate:main deps:core +extern crate core; + extern crate doesnotexist; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate +//- /lib.rs crate:core +"#, + ); + } + + #[test] + fn extern_crate_self_as() { + cov_mark::check!(extern_crate_self_as); + check_diagnostics( + r#" +//- /lib.rs + extern crate doesnotexist; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate +// Should not error. +extern crate self as foo; +struct Foo; +use foo::Foo as Bar; +"#, + ); + } +} -- cgit v1.2.3