Keep (Object) cast on array argument at varargs position#935
Merged
Conversation
An (Object) cast on an array-typed argument at the varargs position is not redundant: it marks the array as a single varargs element rather than being spread, and silences Error Prone's PrimitiveArrayPassedToVarargsMethod. Removing it changes call semantics. Fixes #934
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.
RemoveRedundantTypeCastwas stripping an(Object)cast on an array argument passed to anObject...varargs method, e.g. rewritingsink((Object) new int[]{1, 2, 3})tosink(new int[]{1, 2, 3}).That cast is not redundant:
For a primitive array (
int[]), the cast silences Error Prone'sPrimitiveArrayPassedToVarargsMethod, which is exactly the pattern it exists for (e.g.Method.invoke(null, (Object) new int[]{port})).For a reference-type array (
String[]intoObject...), removing the cast is a silent behaviour change:sink((Object) arr)passes the array as a single element, whereassink(arr)spreads it.The prior fix in Preserve cast on argument to overloaded method in
RemoveRedundantTypeCast#904 only guarded the overload-disambiguation case (Object...vsThrowable); here the varargs method has no competing overload, so that guard didn't apply.Fix
Preserve the cast when it sits at the varargs position, its expression is array-typed, and the cast target is a non-array type. This mirrors the existing guard for a
nullliteral cast at the varargs position.