Skip to content

Commit c642d0c

Browse files
committed
Auto merge of rust-lang#12696 - smoelius:fix-is_test_module_or_function, r=Alexendoo
Fix `is_test_module_or_function` The rustdoc comment for `is_test_module_or_function` states: https://github.com/rust-lang/rust-clippy/blob/2795a6018944a5918b7d276267165484f5d62d6a/clippy_utils/src/lib.rs#L2561-L2566 Given `item`, the function calls `is_in_test_function` with `item.hir_id()`. However, `is_in_test_function` considers only `item`'s parents, not `item` itself. This PR fixes the problem. The `test_with_disallowed_name` test fails without the fix, but passes once applied. changelog: none
2 parents 8eafeeb + de258cc commit c642d0c

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

clippy_utils/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,8 +2505,9 @@ fn with_test_item_names(tcx: TyCtxt<'_>, module: LocalModDefId, f: impl Fn(&[Sym
25052505
/// Note: Add `//@compile-flags: --test` to UI tests with a `#[test]` function
25062506
pub fn is_in_test_function(tcx: TyCtxt<'_>, id: HirId) -> bool {
25072507
with_test_item_names(tcx, tcx.parent_module(id), |names| {
2508-
tcx.hir()
2509-
.parent_iter(id)
2508+
let node = tcx.hir_node(id);
2509+
once((id, node))
2510+
.chain(tcx.hir().parent_iter(id))
25102511
// Since you can nest functions we need to collect all until we leave
25112512
// function scope
25122513
.any(|(_id, node)| {

tests/ui/disallowed_names.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ mod tests {
7171
}
7272
}
7373
}
74+
75+
#[test]
76+
fn test_with_disallowed_name() {
77+
let foo = 0;
78+
}

0 commit comments

Comments
 (0)