Skip to content

Skip var when reassignment relies on declared supertype#1078

Merged
timtebeek merged 2 commits intomainfrom
tim/issue-1077
May 4, 2026
Merged

Skip var when reassignment relies on declared supertype#1078
timtebeek merged 2 commits intomainfrom
tim/issue-1077

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented May 4, 2026

Summary

Test plan

  • ./gradlew test --tests "org.openrewrite.java.migrate.lang.var.*"

When a generic variable is declared as a supertype but initialized with a
concrete subtype constructor or method, switching to `var` narrows the
inferred type and breaks later reassignments. Detect reassignment within
the enclosing method and skip the transformation in that case.

Fixes #1077
The test's `2.1.\d+` regex matched `<johnzon.version>2.1.0</johnzon.version>`
before reaching the jakarta.json-api dependency, so when jakarta.json-api
was released as 2.1.3 the expected/actual diverged. Anchor the regex to
the jakarta.json-api artifact.

Also consolidate the `isReassigned` visitor to use `.reduce(...)` directly,
matching the existing idiom in NullCheck and other recipes.
@timtebeek timtebeek merged commit 4ea1191 into main May 4, 2026
1 check passed
@timtebeek timtebeek deleted the tim/issue-1077 branch May 4, 2026 09:43
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Issue discovered on src/test/java/org/openrewrite/analysis/dataflow/ForwardDataFlowAnalysisTest.java with UseVarForGenericsConstructors

1 participant