mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-21 22:34:05 +00:00
add unqualified_local_imports lint
This commit is contained in:
parent
702987f75b
commit
584c5cf7ae
@ -227,6 +227,8 @@ declare_features! (
|
||||
(internal, staged_api, "1.0.0", None),
|
||||
/// Added for testing unstable lints; perma-unstable.
|
||||
(internal, test_unstable_lint, "1.60.0", None),
|
||||
/// Helps with formatting for `group_imports = "StdExternalCrate"`.
|
||||
(unstable, unqualified_local_imports, "CURRENT_RUSTC_VERSION", None),
|
||||
/// Use for stable + negative coherence and strict coherence depending on trait's
|
||||
/// rustc_strict_coherence value.
|
||||
(unstable, with_negative_coherence, "1.60.0", None),
|
||||
|
@ -899,6 +899,8 @@ lint_unnameable_test_items = cannot test inner items
|
||||
lint_unnecessary_qualification = unnecessary qualification
|
||||
.suggestion = remove the unnecessary path segments
|
||||
|
||||
lint_unqualified_local_imports = `use` of a local item without leading `self::`, `super::`, or `crate::`
|
||||
|
||||
lint_unsafe_attr_outside_unsafe = unsafe attribute used without unsafe
|
||||
.label = usage of unsafe attribute
|
||||
lint_unsafe_attr_outside_unsafe_suggestion = wrap the attribute in `unsafe(...)`
|
||||
|
@ -86,6 +86,7 @@ mod tail_expr_drop_order;
|
||||
mod traits;
|
||||
mod types;
|
||||
mod unit_bindings;
|
||||
mod unqualified_local_imports;
|
||||
mod unused;
|
||||
|
||||
use async_closures::AsyncClosureUsage;
|
||||
@ -126,6 +127,7 @@ use tail_expr_drop_order::TailExprDropOrder;
|
||||
use traits::*;
|
||||
use types::*;
|
||||
use unit_bindings::*;
|
||||
use unqualified_local_imports::*;
|
||||
use unused::*;
|
||||
|
||||
#[rustfmt::skip]
|
||||
@ -249,6 +251,7 @@ late_lint_methods!(
|
||||
TailExprDropOrder: TailExprDropOrder,
|
||||
IfLetRescope: IfLetRescope::default(),
|
||||
StaticMutRefs: StaticMutRefs,
|
||||
UnqualifiedLocalImports: UnqualifiedLocalImports,
|
||||
]
|
||||
]
|
||||
);
|
||||
|
@ -3093,3 +3093,7 @@ pub(crate) enum MutRefSugg {
|
||||
span: Span,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(lint_unqualified_local_imports)]
|
||||
pub(crate) struct UnqualifiedLocalImportsDiag {}
|
||||
|
@ -14,6 +14,8 @@ macro_rules! late_lint_methods {
|
||||
fn check_mod(a: &'tcx rustc_hir::Mod<'tcx>, b: rustc_hir::HirId);
|
||||
fn check_foreign_item(a: &'tcx rustc_hir::ForeignItem<'tcx>);
|
||||
fn check_item(a: &'tcx rustc_hir::Item<'tcx>);
|
||||
/// This is called *after* recursing into the item
|
||||
/// (in contrast to `check_item`, which is checked before).
|
||||
fn check_item_post(a: &'tcx rustc_hir::Item<'tcx>);
|
||||
fn check_local(a: &'tcx rustc_hir::LetStmt<'tcx>);
|
||||
fn check_block(a: &'tcx rustc_hir::Block<'tcx>);
|
||||
@ -135,6 +137,8 @@ macro_rules! early_lint_methods {
|
||||
fn check_crate(a: &rustc_ast::Crate);
|
||||
fn check_crate_post(a: &rustc_ast::Crate);
|
||||
fn check_item(a: &rustc_ast::Item);
|
||||
/// This is called *after* recursing into the item
|
||||
/// (in contrast to `check_item`, which is checked before).
|
||||
fn check_item_post(a: &rustc_ast::Item);
|
||||
fn check_local(a: &rustc_ast::Local);
|
||||
fn check_block(a: &rustc_ast::Block);
|
||||
|
85
compiler/rustc_lint/src/unqualified_local_imports.rs
Normal file
85
compiler/rustc_lint/src/unqualified_local_imports.rs
Normal file
@ -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 {},
|
||||
);
|
||||
}
|
||||
}
|
@ -2058,6 +2058,7 @@ symbols! {
|
||||
unmarked_api,
|
||||
unnamed_fields,
|
||||
unpin,
|
||||
unqualified_local_imports,
|
||||
unreachable,
|
||||
unreachable_2015,
|
||||
unreachable_2015_macro,
|
||||
|
@ -0,0 +1,6 @@
|
||||
//@ check-pass
|
||||
|
||||
#![allow(unqualified_local_imports)]
|
||||
//~^ WARNING unknown lint: `unqualified_local_imports`
|
||||
|
||||
fn main() {}
|
@ -0,0 +1,13 @@
|
||||
warning: unknown lint: `unqualified_local_imports`
|
||||
--> $DIR/feature-gate-unqualified-local-imports.rs:3:1
|
||||
|
|
||||
LL | #![allow(unqualified_local_imports)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: the `unqualified_local_imports` lint is unstable
|
||||
= help: add `#![feature(unqualified_local_imports)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
= note: `#[warn(unknown_lints)]` on by default
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
38
tests/ui/lint/unqualified_local_imports.rs
Normal file
38
tests/ui/lint/unqualified_local_imports.rs
Normal file
@ -0,0 +1,38 @@
|
||||
//@compile-flags: --edition 2018
|
||||
#![feature(unqualified_local_imports)]
|
||||
#![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;
|
||||
|
||||
macro_rules! mymacro {
|
||||
($cond:expr) => {
|
||||
if !$cond {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
}
|
||||
// Macro import: no lint, as there is no other way to write it.
|
||||
pub(crate) use mymacro;
|
||||
|
||||
#[allow(unused)]
|
||||
enum LocalEnum {
|
||||
VarA,
|
||||
VarB,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Import in a function, no lint.
|
||||
use LocalEnum::*;
|
||||
}
|
14
tests/ui/lint/unqualified_local_imports.stderr
Normal file
14
tests/ui/lint/unqualified_local_imports.stderr
Normal file
@ -0,0 +1,14 @@
|
||||
error: `use` of a local item without leading `self::`, `super::`, or `crate::`
|
||||
--> $DIR/unqualified_local_imports.rs:14:5
|
||||
|
|
||||
LL | use localmod::S;
|
||||
| ^^^^^^^^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/unqualified_local_imports.rs:3:9
|
||||
|
|
||||
LL | #![deny(unqualified_local_imports)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
Loading…
Reference in New Issue
Block a user