2021-04-22 04:31:13 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use rustc_ast::{Item, ItemKind, UseTreeKind};
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2021-04-22 04:31:13 -05:00
|
|
|
use rustc_span::symbol::kw;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for imports ending in `::{self}`.
|
2021-04-22 04:31:13 -05:00
|
|
|
///
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-23 00:21:01 -05:00
|
|
|
/// ### Why restrict this?
|
2021-07-29 05:16:06 -05:00
|
|
|
/// In most cases, this can be written much more cleanly by omitting `::{self}`.
|
2021-04-22 04:31:13 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// Removing `::{self}` will cause any non-module items at the same path to also be imported.
|
2021-04-22 04:31:13 -05:00
|
|
|
/// This might cause a naming conflict (https://github.com/rust-lang/rustfmt/issues/3568). This lint makes no attempt
|
|
|
|
/// to detect this scenario and that is why it is a restriction lint.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2021-04-22 04:31:13 -05:00
|
|
|
/// use std::io::{self};
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2021-04-22 04:31:13 -05:00
|
|
|
/// use std::io;
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.53.0"]
|
2021-04-22 04:31:13 -05:00
|
|
|
pub UNNECESSARY_SELF_IMPORTS,
|
|
|
|
restriction,
|
|
|
|
"imports ending in `::{self}`, which can be omitted"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for UnnecessarySelfImports {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let ItemKind::Use(use_tree) = &item.kind
|
2024-04-01 17:26:10 -05:00
|
|
|
&& let UseTreeKind::Nested { items, .. } = &use_tree.kind
|
|
|
|
&& let [(self_tree, _)] = &**items
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let [self_seg] = &*self_tree.prefix.segments
|
|
|
|
&& self_seg.ident.name == kw::SelfLower
|
|
|
|
&& let Some(last_segment) = use_tree.prefix.segments.last()
|
|
|
|
{
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
UNNECESSARY_SELF_IMPORTS,
|
|
|
|
item.span,
|
|
|
|
"import ending with `::{self}`",
|
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
|
|
|
last_segment.span().with_hi(item.span.hi()),
|
|
|
|
"consider omitting `::{self}`",
|
|
|
|
format!(
|
|
|
|
"{}{};",
|
|
|
|
last_segment.ident,
|
|
|
|
if let UseTreeKind::Simple(Some(alias)) = self_tree.kind {
|
|
|
|
format!(" as {alias}")
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
diag.note("this will slightly change semantics; any non-module items at the same path will also be imported");
|
|
|
|
},
|
|
|
|
);
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|