Skip to content

Keep (Object) cast on array argument at varargs position#935

Merged
timtebeek merged 3 commits into
mainfrom
tim/934-varargs-object-cast
Jul 6, 2026
Merged

Keep (Object) cast on array argument at varargs position#935
timtebeek merged 3 commits into
mainfrom
tim/934-varargs-object-cast

Conversation

@timtebeek

@timtebeek timtebeek commented Jul 6, 2026

Copy link
Copy Markdown
Member

RemoveRedundantTypeCast was stripping an (Object) cast on an array argument passed to an Object... varargs method, e.g. rewriting sink((Object) new int[]{1, 2, 3}) to sink(new int[]{1, 2, 3}).

That cast is not redundant:

  • For a primitive array (int[]), the cast silences Error Prone's PrimitiveArrayPassedToVarargsMethod, which is exactly the pattern it exists for (e.g. Method.invoke(null, (Object) new int[]{port})).

  • For a reference-type array (String[] into Object...), removing the cast is a silent behaviour change: sink((Object) arr) passes the array as a single element, whereas sink(arr) spreads it.

  • The prior fix in Preserve cast on argument to overloaded method in RemoveRedundantTypeCast #904 only guarded the overload-disambiguation case (Object... vs Throwable); 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 null literal cast at the varargs position.

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
@timtebeek timtebeek merged commit 4aff898 into main Jul 6, 2026
1 check passed
@timtebeek timtebeek deleted the tim/934-varargs-object-cast branch July 6, 2026 13:17
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

RemoveRedundantTypeCast removes an (Object) cast on a primitive array passed to an Object... varargs method

1 participant