-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use rustc_hir as hir; | ||
use rustc_session::{declare_lint, declare_lint_pass}; | ||
use rustc_span::symbol::kw; | ||
|
||
use crate::{lints, LateContext, LateLintPass, LintContext}; | ||
|
||
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::`. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,edition2018 | ||
/// #![warn(unqualified_local_imports)] | ||
/// | ||
/// mod localmod { | ||
/// pub struct S; | ||
/// } | ||
/// | ||
/// use localmod::S; | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### 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::` imports separately from those | ||
/// refering 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::`. 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::`" | ||
} | ||
|
||
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 }; | ||
// `path` has three resolutions for the type, module, value namespaces. | ||
// However, it shouldn't be possible for those to be in different crates so we only check the first. | ||
let Some(hir::def::Res::Def(_def_kind, def_id)) = path.res.first() else { return }; | ||
if !def_id.is_local() { | ||
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::*` 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; | ||
} | ||
|
||
// This `use` qualifies for our lint! | ||
cx.emit_span_lint( | ||
UNQUALIFIED_LOCAL_IMPORTS, | ||
first_seg.ident.span, | ||
lints::UnqualifiedLocalImportsDiag {}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![deny(unqualified_local_imports)] | ||
|
||
mod localmod { | ||
pub struct S; | ||
pub struct T; | ||
} | ||
|
||
// Not a local import, so no lint. | ||
use std::cell::Cell; | ||
|
||
// Implicitly local import, gets lint. | ||
use localmod::S; //~ERROR: unqualified | ||
|
||
// Explicitly local import, no lint. | ||
use self::localmod::T; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: `use` of a local item without leading `self::`, `super::`, or `crate::` | ||
--> $DIR/unqualified_local_imports.rs:12:5 | ||
| | ||
LL | use localmod::S; | ||
| ^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unqualified_local_imports.rs:1:9 | ||
| | ||
LL | #![deny(unqualified_local_imports)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|