|
| 1 | +use rustc_hir::def::{DefKind,Res}; |
| 2 | +use rustc_hir::{selfas hir}; |
| 3 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 4 | +use rustc_span::symbol::kw; |
| 5 | + |
| 6 | +usecrate::{LateContext,LateLintPass,LintContext, lints}; |
| 7 | + |
| 8 | +declare_lint!{ |
| 9 | +/// The `unqualified_local_imports` lint checks for `use` items that import a local item using a |
| 10 | +/// path that does not start with `self::`, `super::`, or `crate::`. |
| 11 | +/// |
| 12 | +/// ### Example |
| 13 | +/// |
| 14 | +/// ```rust,edition2018 |
| 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 | +/// refering 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. |
| 38 | +pubUNQUALIFIED_LOCAL_IMPORTS, |
| 39 | +Allow, |
| 40 | +"`use` of a local item without leading `self::`, `super::`, or `crate::`", |
| 41 | + @feature_gate = unqualified_local_imports; |
| 42 | +} |
| 43 | + |
| 44 | +declare_lint_pass!(UnqualifiedLocalImports => [UNQUALIFIED_LOCAL_IMPORTS]); |
| 45 | + |
| 46 | +impl<'tcx>LateLintPass<'tcx>forUnqualifiedLocalImports{ |
| 47 | +fncheck_item(&mutself,cx:&LateContext<'tcx>,item:&'tcx hir::Item<'tcx>){ |
| 48 | +let hir::ItemKind::Use(path, _kind) = item.kindelse{return}; |
| 49 | +// `path` has three resolutions for the type, module, value namespaces. |
| 50 | +// Check if any of them qualifies: local crate, and not a macro. |
| 51 | +// (Macros can't be imported any other way so we don't complain about them.) |
| 52 | +let is_local_import = |res:&Res| { |
| 53 | +matches!( |
| 54 | + res, |
| 55 | + hir::def::Res::Def(def_kind, def_id) |
| 56 | +if def_id.is_local() && !matches!(def_kind,DefKind::Macro(_)), |
| 57 | +) |
| 58 | +}; |
| 59 | +if !path.res.iter().any(is_local_import){ |
| 60 | +return; |
| 61 | +} |
| 62 | +// So this does refer to something local. Let's check whether it starts with `self`, |
| 63 | +// `super`, or `crate`. If the path is empty, that means we have a `use *`, which is |
| 64 | +// equivalent to `use crate::*` so we don't fire the lint in that case. |
| 65 | +letSome(first_seg) = path.segments.first()else{return}; |
| 66 | +ifmatches!(first_seg.ident.name, kw::SelfLower | kw::Super | kw::Crate){ |
| 67 | +return; |
| 68 | +} |
| 69 | + |
| 70 | +let encl_item_id = cx.tcx.hir().get_parent_item(item.hir_id()); |
| 71 | +let encl_item = cx.tcx.hir_node_by_def_id(encl_item_id.def_id); |
| 72 | +if encl_item.fn_kind().is_some(){ |
| 73 | +// `use` in a method -- don't lint, that leads to too many undesirable lints |
| 74 | +// when a function imports all variants of an enum. |
| 75 | +return; |
| 76 | +} |
| 77 | + |
| 78 | +// This `use` qualifies for our lint! |
| 79 | + cx.emit_span_lint( |
| 80 | +UNQUALIFIED_LOCAL_IMPORTS, |
| 81 | + first_seg.ident.span, |
| 82 | + lints::UnqualifiedLocalImportsDiag{}, |
| 83 | +); |
| 84 | +} |
| 85 | +} |
0 commit comments