2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-12-06 05:33:31 -06:00
|
|
|
use clippy_utils::is_test_module_or_function;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::{snippet, snippet_with_applicability};
|
2020-01-07 10:53:56 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-14 06:52:08 -06:00
|
|
|
use rustc_hir::{
|
|
|
|
def::{DefKind, Res},
|
2020-05-11 13:23:47 -05:00
|
|
|
Item, ItemKind, PathSegment, UseKind,
|
2020-01-14 06:52:08 -06:00
|
|
|
};
|
2020-01-07 10:53:56 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-02-13 04:30:48 -06:00
|
|
|
use rustc_middle::ty;
|
2020-05-11 13:23:47 -05:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-01-15 03:56:44 -06:00
|
|
|
use rustc_span::symbol::kw;
|
|
|
|
use rustc_span::{sym, BytePos};
|
2020-01-07 10:53:56 -06:00
|
|
|
|
2020-01-14 06:52:08 -06:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `use Enum::*`.
|
2020-01-14 06:52:08 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is usually better style to use the prefixed name of
|
2020-01-14 06:52:08 -06:00
|
|
|
/// an enumeration variant, rather than importing variants.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// Old-style enumerations that prefix the variants are
|
2020-01-14 06:52:08 -06:00
|
|
|
/// still around.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```rust
|
2020-01-14 06:52:08 -06:00
|
|
|
/// use std::cmp::Ordering::*;
|
2022-06-16 10:39:06 -05:00
|
|
|
///
|
|
|
|
/// # fn foo(_: std::cmp::Ordering) {}
|
2020-06-09 09:36:01 -05:00
|
|
|
/// foo(Less);
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2020-06-09 09:36:01 -05:00
|
|
|
/// use std::cmp::Ordering;
|
2022-06-16 10:39:06 -05:00
|
|
|
///
|
|
|
|
/// # fn foo(_: Ordering) {}
|
2020-06-09 09:36:01 -05:00
|
|
|
/// foo(Ordering::Less)
|
2020-01-14 06:52:08 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2020-01-14 06:52:08 -06:00
|
|
|
pub ENUM_GLOB_USE,
|
|
|
|
pedantic,
|
|
|
|
"use items that import all variants of an enum"
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:53:56 -06:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for wildcard imports `use _::*`.
|
2020-01-07 10:53:56 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// wildcard imports can pollute the namespace. This is especially bad if
|
2020-01-07 10:53:56 -06:00
|
|
|
/// you try to import something through a wildcard, that already has been imported by name from
|
|
|
|
/// a different source:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// use crate1::foo; // Imports a function named foo
|
|
|
|
/// use crate2::*; // Has a function named foo
|
|
|
|
///
|
|
|
|
/// foo(); // Calls crate1::foo
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This can lead to confusing error messages at best and to unexpected behavior at worst.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Exceptions
|
2020-05-11 13:23:47 -05:00
|
|
|
/// Wildcard imports are allowed from modules named `prelude`. Many crates (including the standard library)
|
|
|
|
/// provide modules named "prelude" specifically designed for wildcard import.
|
|
|
|
///
|
|
|
|
/// `use super::*` is allowed in test modules. This is defined as any module with "test" in the name.
|
|
|
|
///
|
|
|
|
/// These exceptions can be disabled using the `warn-on-all-wildcard-imports` configuration flag.
|
2020-03-13 12:07:11 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// If macros are imported through the wildcard, this macro is not included
|
2020-01-07 10:53:56 -06:00
|
|
|
/// by the suggestion and has to be added by hand.
|
|
|
|
///
|
2020-02-21 03:15:38 -06:00
|
|
|
/// Applying the suggestion when explicit imports of the things imported with a glob import
|
|
|
|
/// exist, may result in `unused_imports` warnings.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-01-07 10:53:56 -06:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// use crate1::*;
|
|
|
|
///
|
|
|
|
/// foo();
|
|
|
|
/// ```
|
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
2020-01-07 10:53:56 -06:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// use crate1::foo;
|
|
|
|
///
|
|
|
|
/// foo();
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.43.0"]
|
2020-01-07 10:53:56 -06:00
|
|
|
pub WILDCARD_IMPORTS,
|
|
|
|
pedantic,
|
|
|
|
"lint `use _::*` statements"
|
|
|
|
}
|
|
|
|
|
2020-05-11 13:23:47 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WildcardImports {
|
|
|
|
warn_on_all: bool,
|
|
|
|
test_modules_deep: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WildcardImports {
|
|
|
|
pub fn new(warn_on_all: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
warn_on_all,
|
|
|
|
test_modules_deep: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
|
2020-01-07 10:53:56 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl LateLintPass<'_> for WildcardImports {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
2021-07-01 11:17:38 -05:00
|
|
|
if is_test_module_or_function(cx.tcx, item) {
|
2020-05-11 13:23:47 -05:00
|
|
|
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
|
|
|
|
}
|
2022-09-20 00:11:23 -05:00
|
|
|
let module = cx.tcx.parent_module_from_def_id(item.def_id.def_id);
|
|
|
|
if cx.tcx.visibility(item.def_id.def_id) != ty::Visibility::Restricted(module.to_def_id()) {
|
2020-01-07 10:53:56 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if_chain! {
|
|
|
|
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind;
|
2020-05-11 13:23:47 -05:00
|
|
|
if self.warn_on_all || !self.check_exceptions(item, use_path.segments);
|
2022-09-20 00:11:23 -05:00
|
|
|
let used_imports = cx.tcx.names_imported_by_glob_use(item.def_id.def_id);
|
2020-01-09 11:12:54 -06:00
|
|
|
if !used_imports.is_empty(); // Already handled by `unused_imports`
|
2020-01-07 10:53:56 -06:00
|
|
|
then {
|
2020-01-09 11:12:54 -06:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2020-02-21 03:15:38 -06:00
|
|
|
let import_source_snippet = snippet_with_applicability(cx, use_path.span, "..", &mut applicability);
|
|
|
|
let (span, braced_glob) = if import_source_snippet.is_empty() {
|
2020-01-09 11:12:54 -06:00
|
|
|
// This is a `_::{_, *}` import
|
2020-02-21 03:15:38 -06:00
|
|
|
// In this case `use_path.span` is empty and ends directly in front of the `*`,
|
|
|
|
// so we need to extend it by one byte.
|
2020-01-09 11:12:54 -06:00
|
|
|
(
|
|
|
|
use_path.span.with_hi(use_path.span.hi() + BytePos(1)),
|
|
|
|
true,
|
|
|
|
)
|
2020-01-07 10:53:56 -06:00
|
|
|
} else {
|
2020-02-21 03:15:38 -06:00
|
|
|
// In this case, the `use_path.span` ends right before the `::*`, so we need to
|
|
|
|
// extend it up to the `*`. Since it is hard to find the `*` in weird
|
|
|
|
// formattings like `use _ :: *;`, we extend it up to, but not including the
|
|
|
|
// `;`. In nested imports, like `use _::{inner::*, _}` there is no `;` and we
|
|
|
|
// can just use the end of the item span
|
|
|
|
let mut span = use_path.span.with_hi(item.span.hi());
|
|
|
|
if snippet(cx, span, "").ends_with(';') {
|
|
|
|
span = use_path.span.with_hi(item.span.hi() - BytePos(1));
|
|
|
|
}
|
2020-01-09 11:12:54 -06:00
|
|
|
(
|
2020-05-11 13:23:47 -05:00
|
|
|
span, false,
|
2020-01-09 11:12:54 -06:00
|
|
|
)
|
2020-01-07 10:53:56 -06:00
|
|
|
};
|
|
|
|
|
2020-01-09 11:12:54 -06:00
|
|
|
let imports_string = if used_imports.len() == 1 {
|
|
|
|
used_imports.iter().next().unwrap().to_string()
|
|
|
|
} else {
|
|
|
|
let mut imports = used_imports
|
2020-01-07 10:53:56 -06:00
|
|
|
.iter()
|
2020-01-09 11:12:54 -06:00
|
|
|
.map(ToString::to_string)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
imports.sort();
|
|
|
|
if braced_glob {
|
|
|
|
imports.join(", ")
|
2020-01-07 10:53:56 -06:00
|
|
|
} else {
|
2020-01-09 11:12:54 -06:00
|
|
|
format!("{{{}}}", imports.join(", "))
|
2020-01-07 10:53:56 -06:00
|
|
|
}
|
2020-01-09 11:12:54 -06:00
|
|
|
};
|
2020-01-07 10:53:56 -06:00
|
|
|
|
2020-02-21 03:15:38 -06:00
|
|
|
let sugg = if braced_glob {
|
2020-01-09 11:12:54 -06:00
|
|
|
imports_string
|
2020-01-07 10:53:56 -06:00
|
|
|
} else {
|
2022-09-23 12:42:59 -05:00
|
|
|
format!("{import_source_snippet}::{imports_string}")
|
2020-01-09 11:12:54 -06:00
|
|
|
};
|
2020-01-07 10:53:56 -06:00
|
|
|
|
2020-01-14 06:52:08 -06:00
|
|
|
let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res {
|
|
|
|
(ENUM_GLOB_USE, "usage of wildcard import for enum variants")
|
|
|
|
} else {
|
|
|
|
(WILDCARD_IMPORTS, "usage of wildcard import")
|
|
|
|
};
|
|
|
|
|
2020-01-09 11:12:54 -06:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-01-14 06:52:08 -06:00
|
|
|
lint,
|
2020-01-09 11:12:54 -06:00
|
|
|
span,
|
2020-01-14 06:52:08 -06:00
|
|
|
message,
|
2020-01-09 11:12:54 -06:00
|
|
|
"try",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
2020-01-07 10:53:56 -06:00
|
|
|
}
|
2020-01-09 11:12:54 -06:00
|
|
|
}
|
2020-01-07 10:53:56 -06:00
|
|
|
}
|
2020-05-11 13:23:47 -05:00
|
|
|
|
2021-07-01 11:17:38 -05:00
|
|
|
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
|
|
|
if is_test_module_or_function(cx.tcx, item) {
|
2020-05-11 13:23:47 -05:00
|
|
|
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WildcardImports {
|
|
|
|
fn check_exceptions(&self, item: &Item<'_>, segments: &[PathSegment<'_>]) -> bool {
|
2021-12-06 05:33:31 -06:00
|
|
|
item.span.from_expansion()
|
2020-05-11 13:23:47 -05:00
|
|
|
|| is_prelude_import(segments)
|
|
|
|
|| (is_super_only_import(segments) && self.test_modules_deep > 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
// Allow "...prelude::..::*" imports.
|
2020-05-11 13:23:47 -05:00
|
|
|
// Many crates have a prelude, and it is imported as a glob by design.
|
|
|
|
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
|
2021-01-15 03:56:44 -06:00
|
|
|
segments.iter().any(|ps| ps.ident.name == sym::prelude)
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow "super::*" imports in tests.
|
|
|
|
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
|
2021-01-15 03:56:44 -06:00
|
|
|
segments.len() == 1 && segments[0].ident.name == kw::Super
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|