Skip to content

Commit 37e168e

Browse files
committed
Auto merge of #10584 - blyxyas:fix-wildcard_imports_testsrs, r=flip1995
fix: `wildcard_imports` ignore `test.rs` files Adds a check to see if the building crate is a test one, if so, ignore it --- Closes #10580 changelog:[`wildcard_imports`]: Add a check to ignore files named `test.rs` and `tests.rs`
2 parents 3aab0dd + 4a2c025 commit 37e168e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clippy_lints/src/wildcard_imports.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::{
77
def::{DefKind, Res},
88
Item, ItemKind, PathSegment, UseKind,
99
};
10-
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_middle::ty;
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
1313
use rustc_span::symbol::kw;
@@ -102,21 +102,33 @@ declare_clippy_lint! {
102102
pub struct WildcardImports {
103103
warn_on_all: bool,
104104
test_modules_deep: u32,
105+
ignore: bool,
105106
}
106107

107108
impl WildcardImports {
108109
pub fn new(warn_on_all: bool) -> Self {
109110
Self {
110111
warn_on_all,
111112
test_modules_deep: 0,
113+
ignore: false,
112114
}
113115
}
114116
}
115117

116118
impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
117119

118120
impl LateLintPass<'_> for WildcardImports {
121+
fn check_crate(&mut self, cx: &LateContext<'_>) {
122+
if cx.sess().opts.test {
123+
self.ignore = true;
124+
}
125+
}
126+
119127
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
128+
if self.ignore {
129+
return;
130+
}
131+
120132
if is_test_module_or_function(cx.tcx, item) {
121133
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
122134
}

tests/ui/wildcard_imports_cfgtest.rs

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

0 commit comments

Comments
 (0)