2022-06-30 03:50:09 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_hir_and_then;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::snippet;
|
2020-06-23 10:05:22 -05:00
|
|
|
use hir::def::{DefKind, Res};
|
2020-02-26 06:40:31 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast;
|
2020-06-23 10:05:22 -05:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2020-02-26 06:40:31 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-06-23 10:05:22 -05:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-04-08 10:50:13 -05:00
|
|
|
use rustc_span::{edition::Edition, sym, Span};
|
2020-02-26 06:40:31 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `#[macro_use] use...`.
|
2020-02-26 06:40:31 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Since the Rust 2018 edition you can import
|
2020-02-26 06:40:31 -06:00
|
|
|
/// macro's directly, this is considered idiomatic.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-10-09 05:45:29 -05:00
|
|
|
/// ```rust,ignore
|
2020-02-26 06:40:31 -06:00
|
|
|
/// #[macro_use]
|
2020-10-09 05:45:29 -05:00
|
|
|
/// use some_macro;
|
2020-02-26 06:40:31 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.44.0"]
|
2020-03-05 12:22:17 -06:00
|
|
|
pub MACRO_USE_IMPORTS,
|
2020-02-26 06:40:31 -06:00
|
|
|
pedantic,
|
|
|
|
"#[macro_use] is no longer needed"
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:05:22 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
struct PathAndSpan {
|
|
|
|
path: String,
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
2021-05-21 12:35:49 -05:00
|
|
|
/// `MacroRefData` includes the name of the macro.
|
2020-06-23 10:05:22 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MacroRefData {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MacroRefData {
|
2021-05-21 12:35:49 -05:00
|
|
|
pub fn new(name: String) -> Self {
|
|
|
|
Self { name }
|
2020-06-23 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2022-05-21 06:24:00 -05:00
|
|
|
#[expect(clippy::module_name_repetitions)]
|
2020-06-23 10:05:22 -05:00
|
|
|
pub struct MacroUseImports {
|
2022-06-30 03:50:09 -05:00
|
|
|
/// the actual import path used and the span of the attribute above it. The value is
|
|
|
|
/// the location, where the lint should be emitted.
|
|
|
|
imports: Vec<(String, Span, hir::HirId)>,
|
2020-06-23 10:05:22 -05:00
|
|
|
/// the span of the macro reference, kept to ensure only one reference is used per macro call.
|
|
|
|
collected: FxHashSet<Span>,
|
|
|
|
mac_refs: Vec<MacroRefData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
|
|
|
|
|
|
|
|
impl MacroUseImports {
|
2020-06-25 15:41:36 -05:00
|
|
|
fn push_unique_macro(&mut self, cx: &LateContext<'_>, span: Span) {
|
2020-06-23 10:05:22 -05:00
|
|
|
let call_site = span.source_callsite();
|
|
|
|
let name = snippet(cx, cx.sess().source_map().span_until_char(call_site, '!'), "_");
|
2021-09-28 12:03:12 -05:00
|
|
|
if span.source_callee().is_some() && !self.collected.contains(&call_site) {
|
|
|
|
let name = if name.contains("::") {
|
|
|
|
name.split("::").last().unwrap().to_string()
|
|
|
|
} else {
|
|
|
|
name.to_string()
|
|
|
|
};
|
2020-06-23 10:05:22 -05:00
|
|
|
|
2021-09-28 12:03:12 -05:00
|
|
|
self.mac_refs.push(MacroRefData::new(name));
|
|
|
|
self.collected.insert(call_site);
|
2020-06-23 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn push_unique_macro_pat_ty(&mut self, cx: &LateContext<'_>, span: Span) {
|
2020-06-23 10:05:22 -05:00
|
|
|
let call_site = span.source_callsite();
|
|
|
|
let name = snippet(cx, cx.sess().source_map().span_until_char(call_site, '!'), "_");
|
2021-09-28 12:03:12 -05:00
|
|
|
if span.source_callee().is_some() && !self.collected.contains(&call_site) {
|
|
|
|
self.mac_refs.push(MacroRefData::new(name.to_string()));
|
|
|
|
self.collected.insert(call_site);
|
2020-06-23 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
|
2020-02-26 06:40:31 -06:00
|
|
|
if_chain! {
|
2021-01-02 09:29:43 -06:00
|
|
|
if cx.sess().opts.edition >= Edition::Edition2018;
|
2020-06-23 10:05:22 -05:00
|
|
|
if let hir::ItemKind::Use(path, _kind) = &item.kind;
|
2022-06-30 03:50:09 -05:00
|
|
|
let hir_id = item.hir_id();
|
|
|
|
let attrs = cx.tcx.hir().attrs(hir_id);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use));
|
2020-06-23 10:05:22 -05:00
|
|
|
if let Res::Def(DefKind::Mod, id) = path.res;
|
2021-04-22 04:31:13 -05:00
|
|
|
if !id.is_local();
|
2020-02-26 06:40:31 -06:00
|
|
|
then {
|
2021-12-23 02:12:34 -06:00
|
|
|
for kid in cx.tcx.module_children(id).iter() {
|
2020-06-23 10:05:22 -05:00
|
|
|
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
|
|
|
|
let span = mac_attr.span;
|
|
|
|
let def_path = cx.tcx.def_path_str(mac_id);
|
2022-06-30 03:50:09 -05:00
|
|
|
self.imports.push((def_path, span, hir_id));
|
2020-06-23 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-13 06:18:19 -06:00
|
|
|
if item.span.from_expansion() {
|
2020-06-23 10:05:22 -05:00
|
|
|
self.push_unique_macro_pat_ty(cx, item.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &ast::Attribute) {
|
2022-01-13 06:18:19 -06:00
|
|
|
if attr.span.from_expansion() {
|
2020-06-23 10:05:22 -05:00
|
|
|
self.push_unique_macro(cx, attr.span);
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
2022-01-13 06:18:19 -06:00
|
|
|
if expr.span.from_expansion() {
|
2020-06-23 10:05:22 -05:00
|
|
|
self.push_unique_macro(cx, expr.span);
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &hir::Stmt<'_>) {
|
2022-01-13 06:18:19 -06:00
|
|
|
if stmt.span.from_expansion() {
|
2020-06-23 10:05:22 -05:00
|
|
|
self.push_unique_macro(cx, stmt.span);
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &hir::Pat<'_>) {
|
2022-01-13 06:18:19 -06:00
|
|
|
if pat.span.from_expansion() {
|
2020-06-23 10:05:22 -05:00
|
|
|
self.push_unique_macro_pat_ty(cx, pat.span);
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &hir::Ty<'_>) {
|
2022-01-13 06:18:19 -06:00
|
|
|
if ty.span.from_expansion() {
|
2020-06-23 10:05:22 -05:00
|
|
|
self.push_unique_macro_pat_ty(cx, ty.span);
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 04:58:27 -05:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'_>) {
|
2020-06-23 10:05:22 -05:00
|
|
|
let mut used = FxHashMap::default();
|
|
|
|
let mut check_dup = vec![];
|
2022-06-30 03:50:09 -05:00
|
|
|
for (import, span, hir_id) in &self.imports {
|
2020-06-23 10:05:22 -05:00
|
|
|
let found_idx = self.mac_refs.iter().position(|mac| import.ends_with(&mac.name));
|
|
|
|
|
|
|
|
if let Some(idx) = found_idx {
|
2021-02-11 08:04:38 -06:00
|
|
|
self.mac_refs.remove(idx);
|
2020-06-23 10:05:22 -05:00
|
|
|
let seg = import.split("::").collect::<Vec<_>>();
|
|
|
|
|
|
|
|
match seg.as_slice() {
|
|
|
|
// an empty path is impossible
|
|
|
|
// a path should always consist of 2 or more segments
|
|
|
|
[] | [_] => return,
|
|
|
|
[root, item] => {
|
|
|
|
if !check_dup.contains(&(*item).to_string()) {
|
2022-06-30 03:50:09 -05:00
|
|
|
used.entry(((*root).to_string(), span, hir_id))
|
2020-06-23 10:05:22 -05:00
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push((*item).to_string());
|
|
|
|
check_dup.push((*item).to_string());
|
|
|
|
}
|
2021-05-20 05:30:31 -05:00
|
|
|
},
|
2020-06-23 10:05:22 -05:00
|
|
|
[root, rest @ ..] => {
|
|
|
|
if rest.iter().all(|item| !check_dup.contains(&(*item).to_string())) {
|
|
|
|
let filtered = rest
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| {
|
|
|
|
if check_dup.contains(&(*item).to_string()) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((*item).to_string())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2022-06-30 03:50:09 -05:00
|
|
|
used.entry(((*root).to_string(), span, hir_id))
|
2020-06-23 10:05:22 -05:00
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(filtered.join("::"));
|
|
|
|
check_dup.extend(filtered);
|
|
|
|
} else {
|
|
|
|
let rest = rest.to_vec();
|
2022-06-30 03:50:09 -05:00
|
|
|
used.entry(((*root).to_string(), span, hir_id))
|
2020-06-23 10:05:22 -05:00
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(rest.join("::"));
|
|
|
|
check_dup.extend(rest.iter().map(ToString::to_string));
|
|
|
|
}
|
2021-05-20 05:30:31 -05:00
|
|
|
},
|
2020-06-23 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut suggestions = vec![];
|
2022-06-30 03:50:09 -05:00
|
|
|
for ((root, span, hir_id), path) in used {
|
2020-06-23 10:05:22 -05:00
|
|
|
if path.len() == 1 {
|
2022-10-06 02:44:38 -05:00
|
|
|
suggestions.push((span, format!("{root}::{}", path[0]), hir_id));
|
2020-06-23 10:05:22 -05:00
|
|
|
} else {
|
2022-10-06 02:44:38 -05:00
|
|
|
suggestions.push((span, format!("{root}::{{{}}}", path.join(", ")), hir_id));
|
2020-06-23 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mac_refs is not empty we have encountered an import we could not handle
|
|
|
|
// such as `std::prelude::v1::foo` or some other macro that expands to an import.
|
|
|
|
if self.mac_refs.is_empty() {
|
2022-06-30 03:50:09 -05:00
|
|
|
for (span, import, hir_id) in suggestions {
|
2022-10-06 02:44:38 -05:00
|
|
|
let help = format!("use {import};");
|
2022-06-30 03:50:09 -05:00
|
|
|
span_lint_hir_and_then(
|
2020-06-23 10:05:22 -05:00
|
|
|
cx,
|
2020-03-05 12:22:17 -06:00
|
|
|
MACRO_USE_IMPORTS,
|
2022-06-30 03:50:09 -05:00
|
|
|
*hir_id,
|
2020-06-23 10:05:22 -05:00
|
|
|
*span,
|
|
|
|
"`macro_use` attributes are no longer needed in the Rust 2018 edition",
|
2022-06-30 03:50:09 -05:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
|
|
|
*span,
|
|
|
|
"remove the attribute and import the macro directly, try",
|
|
|
|
help,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
},
|
2021-06-03 01:41:37 -05:00
|
|
|
);
|
2020-02-26 06:40:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|