Skip to content

Commit 4c44b4e

Browse files
committed
Remove is_test_module_or_function and use is_in_test instead.
1 parent d2400a4 commit 4c44b4e

11 files changed

+47
-64
lines changed

clippy_lints/src/disallowed_names.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::is_test_module_or_function;
2+
use clippy_utils::is_in_test;
33
use rustc_data_structures::fx::FxHashSet;
4-
use rustc_hir::{Item, Pat, PatKind};
4+
use rustc_hir::{Pat, PatKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::impl_lint_pass;
77

@@ -27,52 +27,30 @@ declare_clippy_lint! {
2727
#[derive(Clone, Debug)]
2828
pub struct DisallowedNames {
2929
disallow: FxHashSet<String>,
30-
test_modules_deep: u32,
3130
}
3231

3332
impl DisallowedNames {
3433
pub fn new(disallowed_names: &[String]) -> Self {
3534
Self {
3635
disallow: disallowed_names.iter().cloned().collect(),
37-
test_modules_deep: 0,
3836
}
3937
}
40-
41-
fn in_test_module(&self) -> bool {
42-
self.test_modules_deep != 0
43-
}
4438
}
4539

4640
impl_lint_pass!(DisallowedNames => [DISALLOWED_NAMES]);
4741

4842
impl<'tcx> LateLintPass<'tcx> for DisallowedNames {
49-
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
50-
if is_test_module_or_function(cx.tcx, item) {
51-
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
52-
}
53-
}
54-
5543
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
56-
// Check whether we are under the `test` attribute.
57-
if self.in_test_module() {
58-
return;
59-
}
60-
61-
if let PatKind::Binding(.., ident, _) = pat.kind {
62-
if self.disallow.contains(&ident.name.to_string()) {
63-
span_lint(
64-
cx,
65-
DISALLOWED_NAMES,
66-
ident.span,
67-
format!("use of a disallowed/placeholder name `{}`", ident.name),
68-
);
69-
}
70-
}
71-
}
72-
73-
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
74-
if is_test_module_or_function(cx.tcx, item) {
75-
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
44+
if let PatKind::Binding(.., ident, _) = pat.kind
45+
&& self.disallow.contains(&ident.name.to_string())
46+
&& !is_in_test(cx.tcx, pat.hir_id)
47+
{
48+
span_lint(
49+
cx,
50+
DISALLOWED_NAMES,
51+
ident.span,
52+
format!("use of a disallowed/placeholder name `{}`", ident.name),
53+
);
7654
}
7755
}
7856
}

clippy_lints/src/wildcard_imports.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_test_module_or_function;
2+
use clippy_utils::is_in_test;
33
use clippy_utils::source::{snippet, snippet_with_applicability};
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_errors::Applicability;
@@ -100,15 +100,13 @@ declare_clippy_lint! {
100100
#[derive(Default)]
101101
pub struct WildcardImports {
102102
warn_on_all: bool,
103-
test_modules_deep: u32,
104103
allowed_segments: FxHashSet<String>,
105104
}
106105

107106
impl WildcardImports {
108107
pub fn new(warn_on_all: bool, allowed_wildcard_imports: FxHashSet<String>) -> Self {
109108
Self {
110109
warn_on_all,
111-
test_modules_deep: 0,
112110
allowed_segments: allowed_wildcard_imports,
113111
}
114112
}
@@ -122,15 +120,12 @@ impl LateLintPass<'_> for WildcardImports {
122120
return;
123121
}
124122

125-
if is_test_module_or_function(cx.tcx, item) {
126-
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
127-
}
128123
let module = cx.tcx.parent_module_from_def_id(item.owner_id.def_id);
129124
if cx.tcx.visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module.to_def_id()) {
130125
return;
131126
}
132127
if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind
133-
&& (self.warn_on_all || !self.check_exceptions(item, use_path.segments))
128+
&& (self.warn_on_all || !self.check_exceptions(cx, item, use_path.segments))
134129
&& let used_imports = cx.tcx.names_imported_by_glob_use(item.owner_id.def_id)
135130
&& !used_imports.is_empty() // Already handled by `unused_imports`
136131
&& !used_imports.contains(&kw::Underscore)
@@ -180,20 +175,14 @@ impl LateLintPass<'_> for WildcardImports {
180175
span_lint_and_sugg(cx, lint, span, message, "try", sugg, applicability);
181176
}
182177
}
183-
184-
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
185-
if is_test_module_or_function(cx.tcx, item) {
186-
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
187-
}
188-
}
189178
}
190179

191180
impl WildcardImports {
192-
fn check_exceptions(&self, item: &Item<'_>, segments: &[PathSegment<'_>]) -> bool {
181+
fn check_exceptions(&self, cx: &LateContext<'_>, item: &Item<'_>, segments: &[PathSegment<'_>]) -> bool {
193182
item.span.from_expansion()
194183
|| is_prelude_import(segments)
195-
|| (is_super_only_import(segments) && self.test_modules_deep > 0)
196184
|| is_allowed_via_config(segments, &self.allowed_segments)
185+
|| (is_super_only_import(segments) && is_in_test(cx.tcx, item.hir_id()))
197186
}
198187
}
199188

tests/ui/disallowed_names.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn issue_1647_ref_mut() {
6060
//~^ ERROR: use of a disallowed/placeholder name `quux`
6161
}
6262

63+
#[cfg(test)]
6364
mod tests {
6465
fn issue_7305() {
6566
// `disallowed_names` lint should not be triggered inside of the test code.

tests/ui/wildcard_imports.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ mod super_imports {
204204
}
205205
}
206206

207+
#[cfg(test)]
207208
mod test_should_pass {
208209
use super::*;
209210

@@ -212,13 +213,15 @@ mod super_imports {
212213
}
213214
}
214215

216+
#[cfg(test)]
215217
mod test_should_pass_inside_function {
216218
fn with_super_inside_function() {
217219
use super::*;
218220
let _ = foofoo();
219221
}
220222
}
221223

224+
#[cfg(test)]
222225
mod test_should_pass_further_inside {
223226
fn insidefoo() {}
224227
mod inner {

tests/ui/wildcard_imports.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ mod super_imports {
205205
}
206206
}
207207

208+
#[cfg(test)]
208209
mod test_should_pass {
209210
use super::*;
210211

@@ -213,13 +214,15 @@ mod super_imports {
213214
}
214215
}
215216

217+
#[cfg(test)]
216218
mod test_should_pass_inside_function {
217219
fn with_super_inside_function() {
218220
use super::*;
219221
let _ = foofoo();
220222
}
221223
}
222224

225+
#[cfg(test)]
223226
mod test_should_pass_further_inside {
224227
fn insidefoo() {}
225228
mod inner {

tests/ui/wildcard_imports.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,31 @@ LL | use super::*;
106106
| ^^^^^^^^ help: try: `super::foofoo`
107107

108108
error: usage of wildcard import
109-
--> tests/ui/wildcard_imports.rs:236:17
109+
--> tests/ui/wildcard_imports.rs:239:17
110110
|
111111
LL | use super::*;
112112
| ^^^^^^^^ help: try: `super::insidefoo`
113113

114114
error: usage of wildcard import
115-
--> tests/ui/wildcard_imports.rs:244:13
115+
--> tests/ui/wildcard_imports.rs:247:13
116116
|
117117
LL | use crate::super_imports::*;
118118
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::super_imports::foofoo`
119119

120120
error: usage of wildcard import
121-
--> tests/ui/wildcard_imports.rs:253:17
121+
--> tests/ui/wildcard_imports.rs:256:17
122122
|
123123
LL | use super::super::*;
124124
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
125125

126126
error: usage of wildcard import
127-
--> tests/ui/wildcard_imports.rs:262:13
127+
--> tests/ui/wildcard_imports.rs:265:13
128128
|
129129
LL | use super::super::super_imports::*;
130130
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
131131

132132
error: usage of wildcard import
133-
--> tests/ui/wildcard_imports.rs:270:13
133+
--> tests/ui/wildcard_imports.rs:273:13
134134
|
135135
LL | use super::*;
136136
| ^^^^^^^^ help: try: `super::foofoo`

tests/ui/wildcard_imports_2021.edition2018.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ mod super_imports {
198198
}
199199
}
200200

201+
#[cfg(test)]
201202
mod test_should_pass {
202203
use super::*;
203204

@@ -206,13 +207,15 @@ mod super_imports {
206207
}
207208
}
208209

210+
#[cfg(test)]
209211
mod test_should_pass_inside_function {
210212
fn with_super_inside_function() {
211213
use super::*;
212214
let _ = foofoo();
213215
}
214216
}
215217

218+
#[cfg(test)]
216219
mod test_should_pass_further_inside {
217220
fn insidefoo() {}
218221
mod inner {

tests/ui/wildcard_imports_2021.edition2018.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,31 @@ LL | use super::*;
106106
| ^^^^^^^^ help: try: `super::foofoo`
107107

108108
error: usage of wildcard import
109-
--> tests/ui/wildcard_imports_2021.rs:230:17
109+
--> tests/ui/wildcard_imports_2021.rs:233:17
110110
|
111111
LL | use super::*;
112112
| ^^^^^^^^ help: try: `super::insidefoo`
113113

114114
error: usage of wildcard import
115-
--> tests/ui/wildcard_imports_2021.rs:238:13
115+
--> tests/ui/wildcard_imports_2021.rs:241:13
116116
|
117117
LL | use crate::super_imports::*;
118118
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::super_imports::foofoo`
119119

120120
error: usage of wildcard import
121-
--> tests/ui/wildcard_imports_2021.rs:247:17
121+
--> tests/ui/wildcard_imports_2021.rs:250:17
122122
|
123123
LL | use super::super::*;
124124
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
125125

126126
error: usage of wildcard import
127-
--> tests/ui/wildcard_imports_2021.rs:256:13
127+
--> tests/ui/wildcard_imports_2021.rs:259:13
128128
|
129129
LL | use super::super::super_imports::*;
130130
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
131131

132132
error: usage of wildcard import
133-
--> tests/ui/wildcard_imports_2021.rs:264:13
133+
--> tests/ui/wildcard_imports_2021.rs:267:13
134134
|
135135
LL | use super::*;
136136
| ^^^^^^^^ help: try: `super::foofoo`

tests/ui/wildcard_imports_2021.edition2021.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ mod super_imports {
198198
}
199199
}
200200

201+
#[cfg(test)]
201202
mod test_should_pass {
202203
use super::*;
203204

@@ -206,13 +207,15 @@ mod super_imports {
206207
}
207208
}
208209

210+
#[cfg(test)]
209211
mod test_should_pass_inside_function {
210212
fn with_super_inside_function() {
211213
use super::*;
212214
let _ = foofoo();
213215
}
214216
}
215217

218+
#[cfg(test)]
216219
mod test_should_pass_further_inside {
217220
fn insidefoo() {}
218221
mod inner {

tests/ui/wildcard_imports_2021.edition2021.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,31 @@ LL | use super::*;
106106
| ^^^^^^^^ help: try: `super::foofoo`
107107

108108
error: usage of wildcard import
109-
--> tests/ui/wildcard_imports_2021.rs:230:17
109+
--> tests/ui/wildcard_imports_2021.rs:233:17
110110
|
111111
LL | use super::*;
112112
| ^^^^^^^^ help: try: `super::insidefoo`
113113

114114
error: usage of wildcard import
115-
--> tests/ui/wildcard_imports_2021.rs:238:13
115+
--> tests/ui/wildcard_imports_2021.rs:241:13
116116
|
117117
LL | use crate::super_imports::*;
118118
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate::super_imports::foofoo`
119119

120120
error: usage of wildcard import
121-
--> tests/ui/wildcard_imports_2021.rs:247:17
121+
--> tests/ui/wildcard_imports_2021.rs:250:17
122122
|
123123
LL | use super::super::*;
124124
| ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo`
125125

126126
error: usage of wildcard import
127-
--> tests/ui/wildcard_imports_2021.rs:256:13
127+
--> tests/ui/wildcard_imports_2021.rs:259:13
128128
|
129129
LL | use super::super::super_imports::*;
130130
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
131131

132132
error: usage of wildcard import
133-
--> tests/ui/wildcard_imports_2021.rs:264:13
133+
--> tests/ui/wildcard_imports_2021.rs:267:13
134134
|
135135
LL | use super::*;
136136
| ^^^^^^^^ help: try: `super::foofoo`

tests/ui/wildcard_imports_2021.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ mod super_imports {
199199
}
200200
}
201201

202+
#[cfg(test)]
202203
mod test_should_pass {
203204
use super::*;
204205

@@ -207,13 +208,15 @@ mod super_imports {
207208
}
208209
}
209210

211+
#[cfg(test)]
210212
mod test_should_pass_inside_function {
211213
fn with_super_inside_function() {
212214
use super::*;
213215
let _ = foofoo();
214216
}
215217
}
216218

219+
#[cfg(test)]
217220
mod test_should_pass_further_inside {
218221
fn insidefoo() {}
219222
mod inner {

0 commit comments

Comments
 (0)