eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Unnecessary cast warnings for method invocation arguments problematic in the presence of polytyping

Open
#4,491 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Follow up from https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4375

The implementation of emission of unnecessary cast warning for method call arguments predates 1.8 and has not been brought up to date with poly expressions and target typing.

Basically `org.eclipse.jdt.internal.compiler.ast.CastExpression.checkNeedForArgumentCasts(BlockScope, Expression, TypeBinding, MethodBinding, Expression[], TypeBinding[], InvocationSite)` and `org.eclipse.jdt.internal.compiler.ast.CastExpression.checkAlternateBinding(BlockScope, Expression, TypeBinding, MethodBinding, Expression[], TypeBinding[], TypeBinding[], InvocationSite)` work by checking the **if the method call would resolve to some other binding when casts of arguments are removed**

If the call is bound to the same method with and without the cast then the cast is redundant is the theory. This work well for Java 1.7- but with 1.8 poly expressions enter the picture. An argument expression may not be a poly expression in the original resolution of the method call - (because it carries a cast and so features in casting context) but may turn out to be a poly expression when the cast is removed and thereby now featuring in invocation context.

Being a poly expression, its type depends on the target type and context must be recomputed - we cant use the type computed in the initial resolution as is to see if the call binds to the same method. That simplistic approach falls flat and was the cause of https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4375

The fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4375 simply recognizes the situation and elides the warning.

The machinery is not enhanced to handle poly expression re-evaluation - so some unnecessary casts may not be diagnosed. Hence this ticket.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.