Skip to content

Commit 86811e9

Browse files
committed
make test module detection more strict
1 parent 11492c7 commit 86811e9

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
@@ -252,11 +252,7 @@ pub fn is_lang_ctor(cx: &LateContext<'_>, qpath: &QPath<'_>, lang_item: LangItem
252252
/// Returns `true` if this `span` was expanded by any macro.
253253
#[must_use]
254254
pub fn in_macro(span: Span) -> bool {
255-
if span.from_expansion() {
256-
!matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
257-
} else {
258-
false
259-
}
255+
span.from_expansion() && !matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
260256
}
261257

262258
pub fn is_unit_expr(expr: &Expr<'_>) -> bool {
@@ -1286,10 +1282,9 @@ pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool
12861282
}
12871283
let enclosing_body = cx.tcx.hir().local_def_id(cx.tcx.hir().enclosing_body_owner(e.hir_id));
12881284
if let Some((Constant::Int(v), _)) = constant(cx, cx.tcx.typeck(enclosing_body), e) {
1289-
value == v
1290-
} else {
1291-
false
1285+
return value == v;
12921286
}
1287+
false
12931288
}
12941289

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

13171312
/// Returns the pre-expansion span if is this comes from an expansion of the
13181313
/// macro `name`.
1319-
/// See also `is_direct_expn_of`.
1314+
/// See also [`is_direct_expn_of`].
13201315
#[must_use]
13211316
pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
13221317
loop {
@@ -1339,13 +1334,13 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
13391334

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

14701465
pub fn is_self_ty(slf: &hir::Ty<'_>) -> bool {
1471-
if_chain! {
1472-
if let TyKind::Path(QPath::Resolved(None, path)) = slf.kind;
1473-
if let Res::SelfTy(..) = path.res;
1474-
then {
1475-
return true
1466+
if let TyKind::Path(QPath::Resolved(None, path)) = slf.kind {
1467+
if let Res::SelfTy(..) = path.res {
1468+
return true;
14761469
}
14771470
}
14781471
false
@@ -2064,15 +2057,12 @@ macro_rules! unwrap_cargo_metadata {
20642057
}
20652058

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

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

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

20902080
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)