Extend UseListOf prose pattern to LinkedHashSet - #1209
Merged
Conversation
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
new LinkedHashSet<>()declaration followed by a chain ofadd(..)calls now collapses tonew LinkedHashSet<>(List.of(..)), keeping the constructed type rather than replacing it; exact-FQN matching still skips subclasses, as #1181 requires.Routing
LinkedHashSetthroughList.ofsidesteps what made Recipe JavaUtilAPIs with LinkedHashSet usage in code result in uncompilable code and unordered set #1181 reject it forUseSetOf:List.ofpermits duplicates, and re-inserting an existing element does not reorder aLinkedHashSet, so the result iterates exactly as the add-chain did. The add matcher widens fromjava.util.List add(..)tojava.util.Collection add(..), sinceSet.adddoes not overrideList.add— this also picks upCollection<T>-declaredArrayListvariables, covered by a new test. The anonymous-class branch is unchanged and remainsArrayList-only.Verified with
./gradlew test --tests 'org.openrewrite.java.migrate.util.*';recipes.csvregenerated for the updated description.