1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// `#![no_std]`: these arrive with the standard prelude and name no path, so a `std::`
// search cannot see them - and a `#[derive]` can use them without the name appearing
// in this file at all, which is why they are not trimmed by inspection.
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use crate::rustc_hir as hir;
use crate::rustc_lint_defs::{declare_lint, declare_lint_pass};
use crate::rustc_span::kw;
use crate::rustc_lint::{LateContext, LateLintPass, LintContext, diagnostics};
declare_lint! {
/// The `unqualified_local_imports` lint checks for `use` items that import a local item using a
/// path that does not start with `self::`, `super::`, or `crate::rustc_lint::`.
///
/// ### Explanation
///
/// This lint is meant to be used with the (unstable) rustfmt setting `group_imports = "StdExternalCrate"`.
/// That setting makes rustfmt group `self::`, `super::`, and `crate::rustc_lint::` imports separately from those
/// referring to other crates. However, rustfmt cannot know whether `use c::S;` refers to a local module `c`
/// or an external crate `c`, so it always gets categorized as an import from another crate.
/// To ensure consistent grouping of imports from the local crate, all local imports must
/// start with `self::`, `super::`, or `crate::rustc_lint::`. This lint can be used to enforce that style.
pub UNQUALIFIED_LOCAL_IMPORTS,
Allow,
"`use` of a local item without leading `self::`, `super::`, or `crate::rustc_lint::`",
@feature_gate = unqualified_local_imports;
}
declare_lint_pass!(UnqualifiedLocalImports => [UNQUALIFIED_LOCAL_IMPORTS]);
impl<'tcx> LateLintPass<'tcx> for UnqualifiedLocalImports {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
let hir::ItemKind::Use(path, _kind) = item.kind else { return };
// Check the type and value namespace resolutions for a local crate.
let is_local_import = matches!(
path.res.type_ns,
Some(hir::def::Res::Def(_, def_id)) if def_id.is_local()
) || matches!(
path.res.value_ns,
Some(hir::def::Res::Def(_, def_id)) if def_id.is_local()
);
if !is_local_import {
return;
}
// So this does refer to something local. Let's check whether it starts with `self`,
// `super`, or `crate`. If the path is empty, that means we have a `use *`, which is
// equivalent to `use crate::rustc_lint::*` so we don't fire the lint in that case.
let Some(first_seg) = path.segments.first() else { return };
if matches!(first_seg.ident.name, kw::SelfLower | kw::Super | kw::Crate) {
return;
}
let encl_item_id = cx.tcx.hir_get_parent_item(item.hir_id());
let encl_item = cx.tcx.hir_node_by_def_id(encl_item_id.def_id);
if encl_item.fn_kind().is_some() {
// `use` in a method -- don't lint, that leads to too many undesirable lints
// when a function imports all variants of an enum.
return;
}
// This `use` qualifies for our lint!
cx.emit_span_lint(
UNQUALIFIED_LOCAL_IMPORTS,
first_seg.ident.span,
diagnostics::UnqualifiedLocalImportsDiag,
);
}
}