From 3366af7925810806380b237a92a1f5fecfa3b9d1 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 16 Feb 2024 21:52:08 -0500 Subject: [PATCH] =?UTF-8?q?Fix=20=F0=9F=A6=B6=F0=9F=94=AB:=20Validate=20`v?= =?UTF-8?q?isitNonNull`=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I often find myself shooting myself in the foot passing the wrong `Cursor` to `visitNonNull`. This is a very easy mistake to make and often leads to quite a bit of confusion. This fixes that by validating that a parent cursor is being passed, not the current cursor. Signed-off-by: Jonathan Leitschuh --- rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java b/rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java index 73d6d853a48..e203928cc1e 100644 --- a/rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java +++ b/rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java @@ -200,6 +200,11 @@ public T visitNonNull(Tree tree, P p) { } public T visitNonNull(Tree tree, P p, Cursor parent) { + if (parent.getValue() instanceof Tree && ((Tree) parent.getValue()).isScope(tree)) { + throw new IllegalArgumentException( + "The `parent` cursor must not point to the same `tree` as the tree to be visited" + ); + } T t = visit(tree, p, parent); assert t != null; return t;