Skip to content

Extend UseListOf prose pattern to LinkedHashSet - #1209

Merged
timtebeek merged 1 commit into
mainfrom
tim/uselistof-linkedhashset
Aug 18, 2026
Merged

Extend UseListOf prose pattern to LinkedHashSet#1209
timtebeek merged 1 commit into
mainfrom
tim/uselistof-linkedhashset

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 18, 2026

Copy link
Copy Markdown
Member
  • A new LinkedHashSet<>() declaration followed by a chain of add(..) calls now collapses to new LinkedHashSet<>(List.of(..)), keeping the constructed type rather than replacing it; exact-FQN matching still skips subclasses, as #1181 requires.

  • Routing LinkedHashSet through List.of sidesteps what made Recipe JavaUtilAPIs with LinkedHashSet usage in code result in uncompilable code and unordered set #1181 reject it for UseSetOf: List.of permits duplicates, and re-inserting an existing element does not reorder a LinkedHashSet, so the result iterates exactly as the add-chain did. The add matcher widens from java.util.List add(..) to java.util.Collection add(..), since Set.add does not override List.add — this also picks up Collection<T>-declared ArrayList variables, covered by a new test. The anonymous-class branch is unchanged and remains ArrayList-only.

Verified with ./gradlew test --tests 'org.openrewrite.java.migrate.util.*'; recipes.csv regenerated for the updated description.

A `new LinkedHashSet<>()` declaration followed by a chain of `add(..)`
calls now collapses to `new LinkedHashSet<>(List.of(..))`, keeping the
constructed type rather than replacing it. Routing through `List.of`
avoids the ordering and duplicate concerns that made #1181 reject
`LinkedHashSet` for `UseSetOf`.

The add matcher widens from `java.util.List add(..)` to
`java.util.Collection add(..)`, since `Set.add` does not override
`List.add`.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Aug 18, 2026
@timtebeek
timtebeek merged commit c5ef164 into main Aug 18, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/uselistof-linkedhashset branch August 18, 2026 19:53
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 18, 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.

1 participant