Fix RemoveRedundantTypeCast removing cast that gives a var lambda its target type#925
Merged
Merged
Conversation
… target type The lambda/member-reference guard only matched when the cast operand was the direct expression. When the operand is wrapped in parentheses (e.g. `(Function<Integer, String>) (i -> ...)`), the operand is a J.ControlParentheses/J.Parentheses, so the guard missed it and the cast was removed. For a `var` local the cast is the sole source of the target type, so removal breaks compilation. Unwrap parentheses before checking for a lambda or method reference. Fixes #924
Contributor
|
Thanks! |
Member
Author
|
Thank you for the report! |
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.
var#924RemoveRedundantTypeCastalready guards against removing a cast whose operand is a lambda or method reference, but the check only matched when the operand was the direct cast expression. When the lambda is wrapped in parentheses —var converter = (Function<Integer, String>) (i -> Integer.toString(i));— the operand is aJ.ControlParentheses/J.Parenthesesnode, so the guard missed it and removed the cast.For a
varlocal the cast is the only thing supplying the lambda's target type, so removing it produces code that does not compile (cannot infer type for local variable).This unwraps parentheses before the lambda / method-reference check. The analogous method-reference form was already preserved (its operand isn't parenthesized); a regression test is added for it too.