Remove redundant cast of optional record property wither methods#1616
Merged
elucash merged 1 commit intoimmutables:masterfrom Nov 11, 2025
werli:fix-build-with-optional
Merged
Remove redundant cast of optional record property wither methods#1616elucash merged 1 commit intoimmutables:masterfrom werli:fix-build-with-optional
elucash merged 1 commit intoimmutables:masterfrom
werli:fix-build-with-optional
Conversation
Member
|
Thank you for the fix! If it is that easy, I wonder why I've overlooked it ;) |
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.
Context
This pull request can be seen as follow up of #1589 (cc @AndreiPurcaru) and tries to unblock using the neat record "wither" interface for optional values as introduced in https://github.com/immutables/immutables/releases/tag/2.11.0 when using strict checkstyle configuration.
I didn't find referencing issues or pull requests. Since the solution looked feasible I just went for it. 🚀
Bug description
generates (among other methods)
optionalis however already of typeOptional<String>so the strict checkstyle configuration in my project fails the build due to the redundant cast.Solution description
Conceptually simple: Only cast when strictly necessary. As stated by the documentation of
org.immutables.value.processor.meta.ValueAttribute#isSafeUncheckedCovariantCast:So the diff for the minimal example above (where the cast is redundant) should look like:
But for cases where the cast is necessary due to dealing with covariants we of course need to retain it:
I opted to use
org.immutables.value.processor.meta.ValueAttribute#consumesWildcardExtendsfor these reasons:suppressCovariantCastForOptional(which is used to generate the unchecked cast warning suppression)Maybe there are better solutions?