Wildcard_imports ignore test.rs files

This commit is contained in:
blyxyas 2023-04-02 01:51:44 +02:00
parent e66488a669
commit 54912410c7
No known key found for this signature in database
GPG key ID: 4D38170B5A2FC334
6 changed files with 101 additions and 1 deletions

View file

@ -7,7 +7,8 @@ use rustc_hir::{
def::{DefKind, Res},
Item, ItemKind, PathSegment, UseKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_hir::{HirId, Mod};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::kw;
@ -102,6 +103,7 @@ declare_clippy_lint! {
pub struct WildcardImports {
warn_on_all: bool,
test_modules_deep: u32,
ignore: bool,
}
impl WildcardImports {
@ -109,6 +111,7 @@ impl WildcardImports {
Self {
warn_on_all,
test_modules_deep: 0,
ignore: false,
}
}
}
@ -116,7 +119,24 @@ impl WildcardImports {
impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
impl LateLintPass<'_> for WildcardImports {
fn check_mod(&mut self, cx: &LateContext<'_>, module: &Mod<'_>, _: HirId) {
let filename = cx
.sess()
.source_map()
.span_to_filename(module.spans.inner_span)
.display(rustc_span::FileNameDisplayPreference::Local)
.to_string();
if filename.ends_with("test.rs") || filename.ends_with("tests.rs") {
self.ignore = true;
}
}
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if self.ignore {
return;
}
if is_test_module_or_function(cx.tcx, item) {
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
}

View file

@ -0,0 +1,18 @@
// run-rustfix
#![warn(clippy::wildcard_imports)]
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
// Test for #10580, the lint should **not** ignore it.
fn foofoo() {}
mod outer {
mod inner {
use super::super::foofoo;
fn barbar() {
let _ = foofoo();
}
}
}
fn main() {}

View file

@ -0,0 +1,18 @@
// run-rustfix
#![warn(clippy::wildcard_imports)]
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
// Test for #10580, the lint should **not** ignore it.
fn foofoo() {}
mod outer {
mod inner {
use super::super::*;
fn barbar() {
let _ = foofoo();
}
}
}
fn main() {}

View file

@ -0,0 +1,10 @@
error: usage of wildcard import
--> $DIR/another_file.rs:11:13
|
LL | use super::super::*;
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
|
= note: `-D clippy::wildcard-imports` implied by `-D warnings`
error: aborting due to previous error

View file

@ -0,0 +1,17 @@
#![warn(clippy::wildcard_imports)]
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
// Test for #10580, the lint **should** not ignore it.
fn foofoo() {}
mod outer {
mod inner {
use super::super::*;
fn barbar() {
let _ = foofoo();
}
}
}
fn main() {}

View file

@ -0,0 +1,17 @@
#![warn(clippy::wildcard_imports)]
#![allow(unused, clippy::unnecessary_wraps, clippy::let_unit_value)]
// Test for #10580, the lint **should** ignore it.
fn foofoo() {}
mod outer {
mod inner {
use super::super::*;
fn barbar() {
let _ = foofoo();
}
}
}
fn main() {}