Auto merge of #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: 2795a60189/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
This commit is contained in:
bors 2024-04-20 15:06:02 +00:00
commit c642d0cab6
2 changed files with 8 additions and 2 deletions

View File

@ -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 /// Note: Add `//@compile-flags: --test` to UI tests with a `#[test]` function
pub fn is_in_test_function(tcx: TyCtxt<'_>, id: HirId) -> bool { pub fn is_in_test_function(tcx: TyCtxt<'_>, id: HirId) -> bool {
with_test_item_names(tcx, tcx.parent_module(id), |names| { with_test_item_names(tcx, tcx.parent_module(id), |names| {
tcx.hir() let node = tcx.hir_node(id);
.parent_iter(id) once((id, node))
.chain(tcx.hir().parent_iter(id))
// Since you can nest functions we need to collect all until we leave // Since you can nest functions we need to collect all until we leave
// function scope // function scope
.any(|(_id, node)| { .any(|(_id, node)| {

View File

@ -71,3 +71,8 @@ fn nested() {
} }
} }
} }
#[test]
fn test_with_disallowed_name() {
let foo = 0;
}