Skip to content

Keep casts that decide whether a conditional is numeric - #1279

Merged
timtebeek merged 1 commit into
mainfrom
keep-casts-in-conditional-branches
Nov 21, 2026
Merged

timtebeek merged 1 commit into
mainfrom
keep-casts-in-conditional-branches

Conversation

@timtebeek

Copy link
Copy Markdown
Member

RemoveRedundantTypeCast removed the casts in isFloat ? (Object) 0.0f : (Object) 0.0d. The casts make this a reference conditional. Without them it is a numeric conditional (JLS 15.25), so the float widens to a double. This broke openrewrite/rewrite-testing-frameworks#1346: JUnit3AssertionsVisitor passed this expression as a J.Literal value, and after the cleanup a 0.0f literal held a Double.

A cast that is the true or false part of a conditional is now kept when it changes the type and either side of the cast is a primitive or boxed type. Same-type casts such as (Integer) value on an Integer are still removed. The local-variable form was already left alone only because no target type was found. The method-argument form fell through to the "cast type is assignable" fallback and lost its casts.

Verified with ./gradlew test --tests '*RemoveRedundantTypeCast*' (56 tests pass, including three new ones in RemoveRedundantTypeCastConditionalTest; without the fix the method-argument case fails). The full ./gradlew test run has no Java failures. The only failures are TypeScript/Python tests whose 8.114.0 snapshot RPC packages could not be installed from npm/PyPI locally.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 21, 2026
@timtebeek
timtebeek merged commit bbca241 into main Nov 21, 2026
1 check passed
@timtebeek
timtebeek deleted the keep-casts-in-conditional-branches branch November 21, 2026 15:58
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant