diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 196ccf9df16..fccd75d8153 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -2505,8 +2505,9 @@ fn with_test_item_names(tcx: TyCtxt<'_>, module: LocalModDefId, f: impl Fn(&[Sym /// Note: Add `//@compile-flags: --test` to UI tests with a `#[test]` function pub fn is_in_test_function(tcx: TyCtxt<'_>, id: HirId) -> bool { with_test_item_names(tcx, tcx.parent_module(id), |names| { - tcx.hir() - .parent_iter(id) + let node = tcx.hir_node(id); + once((id, node)) + .chain(tcx.hir().parent_iter(id)) // Since you can nest functions we need to collect all until we leave // function scope .any(|(_id, node)| { diff --git a/tests/ui/disallowed_names.rs b/tests/ui/disallowed_names.rs index 9a701a2cbcf..13c883409bf 100644 --- a/tests/ui/disallowed_names.rs +++ b/tests/ui/disallowed_names.rs @@ -71,3 +71,8 @@ fn nested() { } } } + +#[test] +fn test_with_disallowed_name() { + let foo = 0; +}