Check if the parent module name contains "test"

This commit is contained in:
Glenn Hope 2020-05-07 08:06:30 -07:00
parent bdc75dbb7b
commit 56f4e1c3a8
4 changed files with 46 additions and 19 deletions

View file

@ -84,7 +84,7 @@ impl LateLintPass<'_, '_> for WildcardImports {
if !in_macro(item.span);
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind;
if !is_prelude_import(use_path.segments);
if !is_super_only_import_in_test(use_path.segments);
if !(is_super_only_import(use_path.segments) && is_in_test_module(cx, item));
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 {
@ -109,8 +109,7 @@ impl LateLintPass<'_, '_> for WildcardImports {
span = use_path.span.with_hi(item.span.hi() - BytePos(1));
}
(
span,
false,
span, false,
)
};
@ -164,8 +163,14 @@ fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
.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")
// Allow "super::*" imports in tests.
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
segments.len() == 1 && segments[0].ident.as_str() == "super"
}
fn is_in_test_module(cx: &LateContext<'_, '_>, item: &Item<'_>) -> bool {
let parent = cx.tcx.hir().get_parent_node(item.hir_id);
let parent_item = cx.tcx.hir().expect_item(parent);
let parent_name = parent_item.ident.name.as_str();
parent_name.contains("test")
}

View file

@ -159,7 +159,15 @@ fn test_weird_formatting() {
mod test_super_imports {
fn foofoo() {}
mod use_super {
mod use_super_should_be_replaced {
use super::foofoo;
fn with_super() {
let _ = foofoo();
}
}
mod use_super_in_test_should_pass {
use super::*;
fn with_super() {
@ -167,7 +175,7 @@ mod test_super_imports {
}
}
mod use_explicit {
mod use_explicit_should_be_replaced {
use test_super_imports::foofoo;
fn with_explicit() {
@ -175,7 +183,7 @@ mod test_super_imports {
}
}
mod use_double_super {
mod use_double_super_should_be_replaced {
mod inner {
use super::super::foofoo;
@ -185,7 +193,7 @@ mod test_super_imports {
}
}
mod use_super_explicit {
mod use_super_explicit_should_be_replaced {
use super::super::test_super_imports::foofoo;
fn with_super_explicit() {

View file

@ -160,7 +160,7 @@ fn test_weird_formatting() {
mod test_super_imports {
fn foofoo() {}
mod use_super {
mod use_super_should_be_replaced {
use super::*;
fn with_super() {
@ -168,7 +168,15 @@ mod test_super_imports {
}
}
mod use_explicit {
mod use_super_in_test_should_pass {
use super::*;
fn with_super() {
let _ = foofoo();
}
}
mod use_explicit_should_be_replaced {
use test_super_imports::*;
fn with_explicit() {
@ -176,7 +184,7 @@ mod test_super_imports {
}
}
mod use_double_super {
mod use_double_super_should_be_replaced {
mod inner {
use super::super::*;
@ -186,7 +194,7 @@ mod test_super_imports {
}
}
mod use_super_explicit {
mod use_super_explicit_should_be_replaced {
use super::super::test_super_imports::*;
fn with_super_explicit() {

View file

@ -93,22 +93,28 @@ LL | | *;
| |_________^ help: try: `crate:: fn_mod::foo`
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:172:13
--> $DIR/wildcard_imports.rs:164:13
|
LL | use super::*;
| ^^^^^^^^ help: try: `super::foofoo`
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:180:13
|
LL | use test_super_imports::*;
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `test_super_imports::foofoo`
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:181:17
--> $DIR/wildcard_imports.rs:189:17
|
LL | use super::super::*;
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
error: usage of wildcard import
--> $DIR/wildcard_imports.rs:190:13
--> $DIR/wildcard_imports.rs:198:13
|
LL | use super::super::test_super_imports::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::test_super_imports::foofoo`
error: aborting due to 18 previous errors
error: aborting due to 19 previous errors