Retain load-bearing type witness in select position in UnnecessaryExplicitTypeArguments#911
Merged
knutwannheden merged 1 commit intoMay 28, 2026
Conversation
…plicitTypeArguments` `UnnecessaryExplicitTypeArguments` removed an explicit type witness from a generic method invocation even when that invocation was the select (receiver) of another method invocation. In that position there is no target type to drive inference, so the witness is load-bearing and removing it produces non-compiling code (the type variable infers to `Object`). Split the enclosing-`J.MethodInvocation` handling into select vs. argument positions. In select position the witness is retained unless the type variables are inferable from the call's own arguments; the existing argument-position and static-method behavior is preserved by reusing the same inference check.
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.
Motivation
UnnecessaryExplicitTypeArgumentsremoves an explicit type witness from a generic method invocation even when that witness is load-bearing — specifically when the invocation is the select (receiver) of another method invocation. In that position there is no target type to drive type inference, so the witness is required, and removing it produces non-compiling code.Observed in the wild on
rewrite-spring'sImplicitWebAnnotationNames:Cursor#getValue()is declared<T> T getValue(). With the<J.VariableDeclarations>witness removed and the result immediately chained into.getVariables(), there is no target type, soTinfers toObject, andObjecthas nogetVariables()method.Summary
enclosing instanceof J.MethodInvocationhandling into select vs. argument positions (enclosingMethod.getSelect() == method).canInferTypeArgumentsFromArguments(...), reused byshouldRetainOnStaticMethodso existing static-method and argument-position behavior is unchanged.Test plan
retainsWitnessWhenResultIsSelectOfMethodInvocationreproducing the chained-call case (recipe makes no change).UnnecessaryExplicitTypeArgumentsTestsuite passes, including the pre-existing select-position caseGenericClass.<T>typedBuilderWithClass(clazz).build()where the witness is still correctly removed (type variable inferable from theClass<T>argument).