Allow 'use super::*;' imports

This commit is contained in:
Glenn Hope 2020-05-03 10:56:25 -07:00
parent 43d9cdc7b3
commit 0c14ea8ed7
4 changed files with 111 additions and 4 deletions

View file

@ -3,7 +3,7 @@ use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
def::{DefKind, Res},
Item, ItemKind, UseKind,
Item, ItemKind, PathSegment, UseKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -83,8 +83,8 @@ impl LateLintPass<'_, '_> for WildcardImports {
if_chain! {
if !in_macro(item.span);
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind;
// don't lint prelude glob imports
if !use_path.segments.iter().last().map_or(false, |ps| ps.ident.as_str() == "prelude");
if !is_prelude_import(use_path.segments);
if !is_super_only_import_in_test(use_path.segments);
let used_imports = cx.tcx.names_imported_by_glob_use(item.hir_id.owner);
if !used_imports.is_empty(); // Already handled by `unused_imports`
then {
@ -154,3 +154,16 @@ impl LateLintPass<'_, '_> for WildcardImports {
}
}
}
// Allow "...prelude::*" imports.
// Many crates have a prelude, and it is imported as a glob by design.
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
segments.iter().last().map_or(false, |ps| ps.ident.as_str() == "prelude")
}
// Allow "super::*" imports.
// This is intended primarily to ease the process of writing unit tests.
fn is_super_only_import_in_test(segments: &[PathSegment<'_>]) -> bool {
segments.iter().len() == 1 &&
segments.first().map_or(false, |ps| ps.ident.as_str() == "super")
}

View file

@ -155,3 +155,41 @@ fn test_weird_formatting() {
exported();
foo();
}
mod test_super_imports {
fn foofoo() {}
mod use_super {
use super::*;
fn with_super() {
let _ = foofoo();
}
}
mod use_explicit {
use test_super_imports::foofoo;
fn with_explicit() {
let _ = foofoo();
}
}
mod use_double_super {
mod inner {
use super::super::foofoo;
fn with_double_super() {
let _ = foofoo();
}
}
}
mod use_super_explicit {
use super::super::test_super_imports::foofoo;
fn with_super_explicit() {
let _ = foofoo();
}
}
}

View file

@ -156,3 +156,41 @@ fn test_weird_formatting() {
exported();
foo();
}
mod test_super_imports {
fn foofoo() {}
mod use_super {
use super::*;
fn with_super() {
let _ = foofoo();
}
}
mod use_explicit {
use test_super_imports::*;
fn with_explicit() {
let _ = foofoo();
}
}
mod use_double_super {
mod inner {
use super::super::*;
fn with_double_super() {
let _ = foofoo();
}
}
}
mod use_super_explicit {
use super::super::test_super_imports::*;
fn with_super_explicit() {
let _ = foofoo();
}
}
}

View file

@ -92,5 +92,23 @@ LL | use crate:: fn_mod::
LL | | *;
| |_________^ help: try: `crate:: fn_mod::foo`
error: aborting due to 15 previous errors
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:172:13
|
LL | use test_super_imports::*;
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `test_super_imports::foofoo`
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:181:17
|
LL | use super::super::*;
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:190:13
|
LL | use super::super::test_super_imports::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::test_super_imports::foofoo`
error: aborting due to 18 previous errors