Skip to content

Commit 3ef3667

Browse files
committed
Auto merge of rust-lang#13049 - Jarcho:is_in_test, r=Alexendoo
Remove `is_in_test_module_or_function` Uses are replaced with `is_in_test` for consistency with other lints and to simplify the implementation of the lints. This means the module name is no longer checked, but that was a horrible hack from a time when late passes couldn't see `#[cfg(..)]` attributes. changelog: none
2 parents e7f2952 + 6d61bda commit 3ef3667

17 files changed

+59
-87
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/functions/impl_trait_in_params.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::is_in_test_function;
2+
use clippy_utils::is_in_test;
33

44
use rustc_hir as hir;
55
use rustc_hir::intravisit::FnKind;
@@ -41,7 +41,7 @@ fn report(cx: &LateContext<'_>, param: &GenericParam<'_>, generics: &Generics<'_
4141
pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: &'tcx Body<'_>, hir_id: HirId) {
4242
if let FnKind::ItemFn(_, generics, _) = kind
4343
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
44-
&& !is_in_test_function(cx.tcx, hir_id)
44+
&& !is_in_test(cx.tcx, hir_id)
4545
{
4646
for param in generics.params {
4747
if param.is_impl_trait() {
@@ -59,7 +59,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
5959
&& of_trait.is_none()
6060
&& let body = cx.tcx.hir().body(body_id)
6161
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
62-
&& !is_in_test_function(cx.tcx, impl_item.hir_id())
62+
&& !is_in_test(cx.tcx, impl_item.hir_id())
6363
{
6464
for param in impl_item.generics.params {
6565
if param.is_impl_trait() {
@@ -75,7 +75,7 @@ pub(super) fn check_trait_item(cx: &LateContext<'_>, trait_item: &TraitItem<'_>,
7575
&& let hir::Node::Item(item) = cx.tcx.parent_hir_node(trait_item.hir_id())
7676
// ^^ (Will always be a trait)
7777
&& !item.vis_span.is_empty() // Is public
78-
&& !is_in_test_function(cx.tcx, trait_item.hir_id())
78+
&& !is_in_test(cx.tcx, trait_item.hir_id())
7979
{
8080
for param in trait_item.generics.params {
8181
if param.is_impl_trait() {

clippy_lints/src/incompatible_msrv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::msrvs::Msrv;
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::is_in_test_function;
3+
use clippy_utils::is_in_test;
44
use rustc_attr::{StabilityLevel, StableSince};
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_hir::{Expr, ExprKind, HirId};
@@ -88,7 +88,7 @@ impl IncompatibleMsrv {
8888
return;
8989
}
9090
let version = self.get_def_id_version(cx.tcx, def_id);
91-
if self.msrv.meets(version) || is_in_test_function(cx.tcx, node) {
91+
if self.msrv.meets(version) || is_in_test(cx.tcx, node) {
9292
return;
9393
}
9494
if let ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) = span.ctxt().outer_expn_data().kind {

clippy_lints/src/methods/unwrap_expect_used.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::{is_never_like, is_type_diagnostic_item};
3-
use clippy_utils::{is_in_cfg_test, is_in_test_function, is_lint_allowed};
3+
use clippy_utils::{is_in_test, is_lint_allowed};
44
use rustc_hir::Expr;
55
use rustc_lint::{LateContext, Lint};
66
use rustc_middle::ty;
@@ -61,7 +61,7 @@ pub(super) fn check(
6161

6262
let method_suffix = if is_err { "_err" } else { "" };
6363

64-
if allow_unwrap_in_tests && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id)) {
64+
if allow_unwrap_in_tests && is_in_test(cx.tcx, expr.hir_id) {
6565
return;
6666
}
6767

clippy_lints/src/missing_assert_message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::is_in_test;
23
use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, PanicExpn};
3-
use clippy_utils::{is_in_cfg_test, is_in_test_function};
44
use rustc_hir::Expr;
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
@@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingAssertMessage {
6262
};
6363

6464
// This lint would be very noisy in tests, so just ignore if we're in test context
65-
if is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id) {
65+
if is_in_test(cx.tcx, expr.hir_id) {
6666
return;
6767
}
6868

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

clippy_lints/src/write.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2+
use clippy_utils::is_in_test;
23
use clippy_utils::macros::{format_arg_removal_span, root_macro_call_first_node, FormatArgsStorage, MacroCall};
34
use clippy_utils::source::{expand_past_previous_comma, snippet_opt};
4-
use clippy_utils::{is_in_cfg_test, is_in_test_function};
55
use rustc_ast::token::LitKind;
66
use rustc_ast::{
77
FormatArgPosition, FormatArgPositionKind, FormatArgs, FormatArgsPiece, FormatOptions, FormatPlaceholder,
@@ -297,8 +297,7 @@ impl<'tcx> LateLintPass<'tcx> for Write {
297297
.as_ref()
298298
.map_or(false, |crate_name| crate_name == "build_script_build");
299299

300-
let allowed_in_tests = self.allow_print_in_tests
301-
&& (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id));
300+
let allowed_in_tests = self.allow_print_in_tests && is_in_test(cx.tcx, expr.hir_id);
302301
match diag_name {
303302
sym::print_macro | sym::println_macro if !allowed_in_tests => {
304303
if !is_build_script {

clippy_utils/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,16 +2625,6 @@ pub fn inherits_cfg(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
26252625
.any(|attr| attr.has_name(sym::cfg))
26262626
}
26272627

2628-
/// Checks whether item either has `test` attribute applied, or
2629-
/// is a module with `test` in its name.
2630-
///
2631-
/// Note: Add `//@compile-flags: --test` to UI tests with a `#[test]` function
2632-
pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
2633-
is_in_test_function(tcx, item.hir_id())
2634-
|| matches!(item.kind, ItemKind::Mod(..))
2635-
&& item.ident.name.as_str().split('_').any(|a| a == "test" || a == "tests")
2636-
}
2637-
26382628
/// Walks up the HIR tree from the given expression in an attempt to find where the value is
26392629
/// consumed.
26402630
///

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 {

0 commit comments

Comments
 (0)