Skip to content

Commit

Permalink
Fix 🦶🔫: Validate visitNonNull arguments (#4016)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
JLLeitschuh authored Feb 19, 2024
1 parent dd77edd commit b077208
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b077208

Please sign in to comment.