about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-20 15:06:02 +0000
committerbors <bors@rust-lang.org>2024-04-20 15:06:02 +0000
commitc642d0cab63546ce094b75f2d31e0639d9b12399 (patch)
tree16b9f31fec070f63eae19eaf855e1e71f38848f0
parent8eafeeb47ac5605f17eb473e1c011060e49751d9 (diff)
parentde258cc6becde010381502ae0e80b4aca4064e3c (diff)
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: 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
-rw-r--r--clippy_utils/src/lib.rs5
-rw-r--r--tests/ui/disallowed_names.rs5
2 files changed, 8 insertions, 2 deletions
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 @@ mod tests {
         }
     }
 }
+
+#[test]
+fn test_with_disallowed_name() {
+    let foo = 0;
+}