Skip to content

Commit 0aa9823

Browse files
committed
Wildcard_imports ignore test.rs files
1 parent ac4838c commit 0aa9823

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

clippy_lints/src/wildcard_imports.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use rustc_hir::{
77
def::{DefKind, Res},
88
Item, ItemKind, PathSegment, UseKind,
99
};
10-
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_hir::{HirId, Mod};
11+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1112
use rustc_middle::ty;
1213
use rustc_session::{declare_tool_lint, impl_lint_pass};
1314
use rustc_span::symbol::kw;
@@ -102,21 +103,40 @@ declare_clippy_lint! {
102103
pub struct WildcardImports {
103104
warn_on_all: bool,
104105
test_modules_deep: u32,
106+
ignore: bool,
105107
}
106108

107109
impl WildcardImports {
108110
pub fn new(warn_on_all: bool) -> Self {
109111
Self {
110112
warn_on_all,
111113
test_modules_deep: 0,
114+
ignore: false,
112115
}
113116
}
114117
}
115118

116119
impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
117120

118121
impl LateLintPass<'_> for WildcardImports {
122+
fn check_mod(&mut self, cx: &LateContext<'_>, module: &Mod<'_>, _: HirId) {
123+
let filename = cx
124+
.sess()
125+
.source_map()
126+
.span_to_filename(module.spans.inner_span)
127+
.display(rustc_span::FileNameDisplayPreference::Local)
128+
.to_string();
129+
130+
if filename.ends_with("test.rs") || filename.ends_with("tests.rs") {
131+
self.ignore = true;
132+
}
133+
}
134+
119135
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
136+
if self.ignore {
137+
return;
138+
}
139+
120140
if is_test_module_or_function(cx.tcx, item) {
121141
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
122142
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
#![warn(clippy::wildcard_imports)]
3+
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
4+
5+
// Test for #10580, the lint should **not** ignore it.
6+
7+
fn foofoo() {}
8+
9+
mod outer {
10+
mod inner {
11+
use super::super::foofoo;
12+
fn barbar() {
13+
let _ = foofoo();
14+
}
15+
}
16+
}
17+
18+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
#![warn(clippy::wildcard_imports)]
3+
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
4+
5+
// Test for #10580, the lint should **not** ignore it.
6+
7+
fn foofoo() {}
8+
9+
mod outer {
10+
mod inner {
11+
use super::super::*;
12+
fn barbar() {
13+
let _ = foofoo();
14+
}
15+
}
16+
}
17+
18+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: usage of wildcard import
2+
--> $DIR/another_file.rs:11:13
3+
|
4+
LL | use super::super::*;
5+
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
6+
|
7+
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

tests/ui/wildcard_imports/test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![warn(clippy::wildcard_imports)]
2+
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
3+
4+
// Test for #10580, the lint **should** not ignore it.
5+
6+
fn foofoo() {}
7+
8+
mod outer {
9+
mod inner {
10+
use super::super::*;
11+
fn barbar() {
12+
let _ = foofoo();
13+
}
14+
}
15+
}
16+
17+
fn main() {}

tests/ui/wildcard_imports/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![warn(clippy::wildcard_imports)]
2+
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
3+
4+
// Test for #10580, the lint **should** ignore it.
5+
6+
fn foofoo() {}
7+
8+
mod outer {
9+
mod inner {
10+
use super::super::*;
11+
fn barbar() {
12+
let _ = foofoo();
13+
}
14+
}
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)