From 9a22056b3fd485db57000ce7336ca00dae501b0b Mon Sep 17 00:00:00 2001 From: martinfrancois Date: Tue, 11 Aug 2026 20:29:35 +0200 Subject: [PATCH] AllBranchesIdentical: add failing tests for deleted array access and cast doNotChangeWhenConditionContainsArrayAccess pins that collapsing branches must not delete an array access whose evaluation may throw ArrayIndexOutOfBoundsException. doNotChangeWhenConditionContainsCast pins the same for a cast that may throw ClassCastException. SideEffects.mayHaveSideEffects reports both as effect-free, so both are marked @ExpectedToFail until the shared helper covers them. --- .../AllBranchesIdenticalTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/org/openrewrite/staticanalysis/AllBranchesIdenticalTest.java b/src/test/java/org/openrewrite/staticanalysis/AllBranchesIdenticalTest.java index add79971c..bcafad2f3 100644 --- a/src/test/java/org/openrewrite/staticanalysis/AllBranchesIdenticalTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/AllBranchesIdenticalTest.java @@ -16,6 +16,7 @@ package org.openrewrite.staticanalysis; import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.ExpectedToFail; import org.openrewrite.DocumentExample; import org.openrewrite.Issue; import org.openrewrite.test.RecipeSpec; @@ -315,4 +316,46 @@ void test(Iterator it) { ) ); } + + @ExpectedToFail("SideEffects.mayHaveSideEffects treats array access as effect-free, so the collapse deletes an access that may throw ArrayIndexOutOfBoundsException") + @Test + void doNotChangeWhenConditionContainsArrayAccess() { + rewriteRun( + //language=java + java( + """ + class Test { + void test(boolean[] flags) { + if (flags[5]) { + System.out.println("hello"); + } else { + System.out.println("hello"); + } + } + } + """ + ) + ); + } + + @ExpectedToFail("SideEffects.mayHaveSideEffects treats casts as effect-free, so the collapse deletes a cast that may throw ClassCastException, and with it the unboxing that may throw NullPointerException") + @Test + void doNotChangeWhenConditionContainsCast() { + rewriteRun( + //language=java + java( + """ + class Test { + void test(Object o) { + if ((Boolean) o) { + System.out.println("hello"); + } else { + System.out.println("hello"); + } + } + } + """ + ) + ); + } }