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

Warn when 'null' argument may lead to "unexpected" overload resolution

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

Description

As discussed in https://github.com/eclipse-equinox/equinox/pull/216#issuecomment-1429299577 :

In that particular case, people were surprised when `String.valueOf(null)` resolves to `valueOf(char[])`, not `valueOf(Object)`.

If we are able to define a precise condition under which [JLS 15.12.2.5](https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.12.2.5) gives different results than what intuition would suggest, then perhaps the compiler could issue an optional warning.

Proposal:
* overload resolution finds more than one applicable method and requires determination of the most specific among them
* one argument is 'null' (or should we consider all arguments of type `Object`?)
* the corresponding parameters of applicable methods include one of type `Object` plus another type (any type? only array types?)
* Warn only, when the parameter at this particular rank is the only discriminator between overloads?

Alternatively, we could even stop at the first condition and _always_ report when several methods are applicable. Only few people really understand overload resolution based on most-specific analysis anyway, and even fewer people are aware of the fact that in this situation innocent refactorings may change program behavior.

Yet another idea would be, to _positively_ define which overload situations can be considered as _harmless_, free of any surprises. I don't really know which set is larger: safe uses or dangerous uses of overloading.

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.