-
Notifications
You must be signed in to change notification settings - Fork 13.4k
add unqualified_local_imports lint #125645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
use rustc_hir::def::{DefKind, Res}; | ||
use rustc_hir::{self as hir}; | ||
use rustc_session::{declare_lint, declare_lint_pass}; | ||
use rustc_span::symbol::kw; | ||
|
||
use crate::{LateContext, LateLintPass, LintContext, lints}; | ||
|
||
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; | ||
/// # // We have to actually use `S`, or else the `unused` warnings suppress the lint we care about. | ||
/// # pub fn main() { | ||
/// # let _x = 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::`", | ||
@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 }; | ||
// `path` has three resolutions for the type, module, value namespaces. | ||
// Check if any of them qualifies: local crate, and not a macro. | ||
// (Macros can't be imported any other way so we don't complain about them.) | ||
let is_local_import = |res: &Res| { | ||
matches!( | ||
res, | ||
hir::def::Res::Def(def_kind, def_id) | ||
if def_id.is_local() && !matches!(def_kind, DefKind::Macro(_)), | ||
) | ||
}; | ||
if !path.res.iter().any(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::*` 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, | ||
lints::UnqualifiedLocalImportsDiag {}, | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.