Skip to content

Commit a04a7cd

Browse files
committed
Auto merge of #7779 - rust-lang:test_module, r=flip1995
make test module detection more strict I started with some small improvements to clippy_utils/src/lib.rs, but then found that our "test" module detection would also catch words containing "test" like e.g. "attestation". So I made this a bit more strict (splitting by `'_'` and checking for `test` or `tests`), adding a test case as I went. --- *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: none
2 parents b7f3f7f + 86811e9 commit a04a7cd

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

clippy_utils/src/lib.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ pub fn is_lang_ctor(cx: &LateContext<'_>, qpath: &QPath<'_>, lang_item: LangItem
251251
/// Returns `true` if this `span` was expanded by any macro.
252252
#[must_use]
253253
pub fn in_macro(span: Span) -> bool {
254-
if span.from_expansion() {
255-
!matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
256-
} else {
257-
false
258-
}
254+
span.from_expansion() && !matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
259255
}
260256

261257
pub fn is_unit_expr(expr: &Expr<'_>) -> bool {
@@ -1285,10 +1281,9 @@ pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool
12851281
}
12861282
let enclosing_body = cx.tcx.hir().local_def_id(cx.tcx.hir().enclosing_body_owner(e.hir_id));
12871283
if let Some((Constant::Int(v), _)) = constant(cx, cx.tcx.typeck(enclosing_body), e) {
1288-
value == v
1289-
} else {
1290-
false
1284+
return value == v;
12911285
}
1286+
false
12921287
}
12931288

12941289
/// Checks whether the given expression is a constant literal of the given value.
@@ -1315,7 +1310,7 @@ pub fn is_adjusted(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
13151310

13161311
/// Returns the pre-expansion span if is this comes from an expansion of the
13171312
/// macro `name`.
1318-
/// See also `is_direct_expn_of`.
1313+
/// See also [`is_direct_expn_of`].
13191314
#[must_use]
13201315
pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
13211316
loop {
@@ -1338,13 +1333,13 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
13381333

13391334
/// Returns the pre-expansion span if the span directly comes from an expansion
13401335
/// of the macro `name`.
1341-
/// The difference with `is_expn_of` is that in
1342-
/// ```rust,ignore
1336+
/// The difference with [`is_expn_of`] is that in
1337+
/// ```rust
1338+
/// # macro_rules! foo { ($e:tt) => { $e } }; macro_rules! bar { ($e:expr) => { $e } }
13431339
/// foo!(bar!(42));
13441340
/// ```
13451341
/// `42` is considered expanded from `foo!` and `bar!` by `is_expn_of` but only
1346-
/// `bar!` by
1347-
/// `is_direct_expn_of`.
1342+
/// from `bar!` by `is_direct_expn_of`.
13481343
#[must_use]
13491344
pub fn is_direct_expn_of(span: Span, name: &str) -> Option<Span> {
13501345
if span.from_expansion() {
@@ -1467,11 +1462,9 @@ pub fn is_self(slf: &Param<'_>) -> bool {
14671462
}
14681463

14691464
pub fn is_self_ty(slf: &hir::Ty<'_>) -> bool {
1470-
if_chain! {
1471-
if let TyKind::Path(QPath::Resolved(None, path)) = slf.kind;
1472-
if let Res::SelfTy(..) = path.res;
1473-
then {
1474-
return true
1465+
if let TyKind::Path(QPath::Resolved(None, path)) = slf.kind {
1466+
if let Res::SelfTy(..) = path.res {
1467+
return true;
14751468
}
14761469
}
14771470
false
@@ -2063,15 +2056,12 @@ macro_rules! unwrap_cargo_metadata {
20632056
}
20642057

20652058
pub fn is_hir_ty_cfg_dependant(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
2066-
if_chain! {
2067-
if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind;
2068-
if let Res::Def(_, def_id) = path.res;
2069-
then {
2070-
cx.tcx.has_attr(def_id, sym::cfg) || cx.tcx.has_attr(def_id, sym::cfg_attr)
2071-
} else {
2072-
false
2059+
if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind {
2060+
if let Res::Def(_, def_id) = path.res {
2061+
return cx.tcx.has_attr(def_id, sym::cfg) || cx.tcx.has_attr(def_id, sym::cfg_attr);
20732062
}
20742063
}
2064+
false
20752065
}
20762066

20772067
/// Checks whether item either has `test` attribute applied, or
@@ -2083,7 +2073,7 @@ pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
20832073
}
20842074
}
20852075

2086-
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
2076+
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().split('_').any(|a| a == "test" || a == "tests")
20872077
}
20882078

20892079
macro_rules! op_utils {

tests/ui/wildcard_imports.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,12 @@ mod super_imports {
230230
let _ = foofoo();
231231
}
232232
}
233+
234+
mod attestation_should_be_replaced {
235+
use super::foofoo;
236+
237+
fn with_explicit() {
238+
let _ = foofoo();
239+
}
240+
}
233241
}

tests/ui/wildcard_imports.rs

+8
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,12 @@ mod super_imports {
231231
let _ = foofoo();
232232
}
233233
}
234+
235+
mod attestation_should_be_replaced {
236+
use super::*;
237+
238+
fn with_explicit() {
239+
let _ = foofoo();
240+
}
241+
}
234242
}

tests/ui/wildcard_imports.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,11 @@ error: usage of wildcard import
122122
LL | use super::super::super_imports::*;
123123
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo`
124124

125-
error: aborting due to 20 previous errors
125+
error: usage of wildcard import
126+
--> $DIR/wildcard_imports.rs:236:13
127+
|
128+
LL | use super::*;
129+
| ^^^^^^^^ help: try: `super::foofoo`
130+
131+
error: aborting due to 21 previous errors
126132

0 commit comments

Comments
 (0)