1use rustc_hir::{selfas hir};
2use rustc_session::{declare_lint, declare_lint_pass};
3use rustc_span::kw;
45use crate::{LateContext, LateLintPass, LintContext, lints};
67#[doc =
r" The `unqualified_local_imports` lint checks for `use` items that import a local item using a"]
#[doc = r" path that does not start with `self::`, `super::`, or `crate::`."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,edition2018"]
#[doc = r" #![feature(unqualified_local_imports)]"]
#[doc = r" #![warn(unqualified_local_imports)]"]
#[doc = r""]
#[doc = r" mod localmod {"]
#[doc = r" pub struct S;"]
#[doc = r" }"]
#[doc = r""]
#[doc = r" use localmod::S;"]
#[doc =
r" # // We have to actually use `S`, or else the `unused` warnings suppress the lint we care about."]
#[doc = r" # pub fn main() {"]
#[doc = r" # let _x = S;"]
#[doc = r" # }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r#" This lint is meant to be used with the (unstable) rustfmt setting `group_imports = "StdExternalCrate"`."#]
#[doc =
r" That setting makes rustfmt group `self::`, `super::`, and `crate::` imports separately from those"]
#[doc =
r" referring to other crates. However, rustfmt cannot know whether `use c::S;` refers to a local module `c`"]
#[doc =
r" or an external crate `c`, so it always gets categorized as an import from another crate."]
#[doc =
r" To ensure consistent grouping of imports from the local crate, all local imports must"]
#[doc =
r" start with `self::`, `super::`, or `crate::`. This lint can be used to enforce that style."]
pub static UNQUALIFIED_LOCAL_IMPORTS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNQUALIFIED_LOCAL_IMPORTS",
default_level: ::rustc_lint_defs::Allow,
desc: "`use` of a local item without leading `self::`, `super::`, or `crate::`",
is_externally_loaded: false,
feature_gate: Some(rustc_span::sym::unqualified_local_imports),
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
8/// The `unqualified_local_imports` lint checks for `use` items that import a local item using a
9 /// path that does not start with `self::`, `super::`, or `crate::`.
10 ///
11 /// ### Example
12 ///
13 /// ```rust,edition2018
14 /// #![feature(unqualified_local_imports)]
15 /// #![warn(unqualified_local_imports)]
16 ///
17 /// mod localmod {
18 /// pub struct S;
19 /// }
20 ///
21 /// use localmod::S;
22 /// # // We have to actually use `S`, or else the `unused` warnings suppress the lint we care about.
23 /// # pub fn main() {
24 /// # let _x = S;
25 /// # }
26 /// ```
27 ///
28 /// {{produces}}
29 ///
30 /// ### Explanation
31 ///
32 /// This lint is meant to be used with the (unstable) rustfmt setting `group_imports = "StdExternalCrate"`.
33 /// That setting makes rustfmt group `self::`, `super::`, and `crate::` imports separately from those
34 /// referring to other crates. However, rustfmt cannot know whether `use c::S;` refers to a local module `c`
35 /// or an external crate `c`, so it always gets categorized as an import from another crate.
36 /// To ensure consistent grouping of imports from the local crate, all local imports must
37 /// start with `self::`, `super::`, or `crate::`. This lint can be used to enforce that style.
38pub UNQUALIFIED_LOCAL_IMPORTS,
39 Allow,
40"`use` of a local item without leading `self::`, `super::`, or `crate::`",
41 @feature_gate = unqualified_local_imports;
42}4344pub struct UnqualifiedLocalImports;
#[automatically_derived]
impl ::core::marker::Copy for UnqualifiedLocalImports { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnqualifiedLocalImports { }
#[automatically_derived]
impl ::core::clone::Clone for UnqualifiedLocalImports {
#[inline]
fn clone(&self) -> UnqualifiedLocalImports { *self }
}
impl ::rustc_lint_defs::LintPass for UnqualifiedLocalImports {
fn name(&self) -> &'static str { "UnqualifiedLocalImports" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([UNQUALIFIED_LOCAL_IMPORTS]))
}
}
impl UnqualifiedLocalImports {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([UNQUALIFIED_LOCAL_IMPORTS]))
}
}declare_lint_pass!(UnqualifiedLocalImports => [UNQUALIFIED_LOCAL_IMPORTS]);
4546impl<'tcx> LateLintPass<'tcx> for UnqualifiedLocalImports {
47fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
48let hir::ItemKind::Use(path, _kind) = item.kind else { return };
49// Check the type and value namespace resolutions for a local crate.
50let is_local_import = #[allow(non_exhaustive_omitted_patterns)] match path.res.type_ns {
Some(hir::def::Res::Def(_, def_id)) if def_id.is_local() => true,
_ => false,
}matches!(
51 path.res.type_ns,
52Some(hir::def::Res::Def(_, def_id)) if def_id.is_local()
53 ) || #[allow(non_exhaustive_omitted_patterns)] match path.res.value_ns {
Some(hir::def::Res::Def(_, def_id)) if def_id.is_local() => true,
_ => false,
}matches!(
54 path.res.value_ns,
55Some(hir::def::Res::Def(_, def_id)) if def_id.is_local()
56 );
57if !is_local_import {
58return;
59 }
60// So this does refer to something local. Let's check whether it starts with `self`,
61 // `super`, or `crate`. If the path is empty, that means we have a `use *`, which is
62 // equivalent to `use crate::*` so we don't fire the lint in that case.
63let Some(first_seg) = path.segments.first() else { return };
64if #[allow(non_exhaustive_omitted_patterns)] match first_seg.ident.name {
kw::SelfLower | kw::Super | kw::Crate => true,
_ => false,
}matches!(first_seg.ident.name, kw::SelfLower | kw::Super | kw::Crate) {
65return;
66 }
6768let encl_item_id = cx.tcx.hir_get_parent_item(item.hir_id());
69let encl_item = cx.tcx.hir_node_by_def_id(encl_item_id.def_id);
70if encl_item.fn_kind().is_some() {
71// `use` in a method -- don't lint, that leads to too many undesirable lints
72 // when a function imports all variants of an enum.
73return;
74 }
7576// This `use` qualifies for our lint!
77cx.emit_span_lint(
78UNQUALIFIED_LOCAL_IMPORTS,
79first_seg.ident.span,
80 lints::UnqualifiedLocalImportsDiag {},
81 );
82 }
83}