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;