Bug 491454 - Necessary cast wrongly flagged as unnecessary
Summary: Necessary cast wrongly flagged as unnecessary
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.6   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-11 12:59 EDT by Roland Illig CLA
Modified: 2023-12-28 02:37 EST (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Roland Illig CLA 2016-04-11 12:59:44 EDT
public class Tmp {

  static <T> T getValue() {
    return null;
  }

  public static void main(String[] args) {
    // String.valueOf((Object) getValue());
    String.valueOf(getValue());
  }
}


The above code calls String.valueOf(char[]), which is ok, since there are two candidate functions for a one-parameter Object call.

When I want to change this call to String.valueOf(Object), I can add a cast in front of the getValue() call.

But then, when cleaning up including “Remove unnecessary casts”, this cast is removed. It should stay though, since it is not unnecessary.

In general, is it possible that the cleanup actions compare the bytecode of the cleaned up classes before and after the change, to make sure nothing essential has been changed?
Comment 1 Jay Arthanareeswaran CLA 2016-04-12 02:46:55 EDT
Moving to UI for investigation.
Comment 2 Noopur Gupta CLA 2016-04-12 04:09:41 EDT
String.valueOf((Object) getValue()) invokes the String.valueOf(Object) method, so the cast is actually unnecessary and should be removed by the clean up.

String.valueOf(getValue()) in this example should also invoke String.valueOf(Object) method, which is the case when using JDK7. But it calls String.valueOf(char[]) if using JDK8. Looks like this is the issue that should be fixed. 

Moving back to JDT Core for analysis.
Comment 3 Jay Arthanareeswaran CLA 2016-04-12 04:34:53 EDT
(In reply to Noopur Gupta from comment #2)
> String.valueOf(getValue()) in this example should also invoke
> String.valueOf(Object) method, which is the case when using JDK7. But it
> calls String.valueOf(char[]) if using JDK8. Looks like this is the issue
> that should be fixed. 
> 
> Moving back to JDT Core for analysis.

This is the expected behavior according to bug 487118.
Comment 4 Roland Illig CLA 2016-04-12 05:25:09 EDT
According to my understanding of “cleanup”, this should not be the expected behavior.

In the example code given in the first comment, when I remove the "//", I, as the programmer, have decided that I want to call getValue(), then cast the returned value to Object, and then call String.valueOf(Object).

Cleaning up code means to me that unnecessary syntactical elements are removed or beautified, and that the behavior of the code does not change.

But when the cast is removed, the selected String.valueOf method changes, which is unexpected to me.

Is there an general agreement on what “cleanup” means in the JDT context? Maybe it’s a different definition than mine.
Comment 5 Noopur Gupta CLA 2016-04-12 05:42:22 EDT
(In reply to Roland Illig from comment #4)
> But when the cast is removed, the selected String.valueOf method changes,
> which is unexpected to me.

Correct, this is an issue (after bug 487118).

(In reply to Jay Arthanareeswaran from comment #3)
> (In reply to Noopur Gupta from comment #2)
> > String.valueOf(getValue()) in this example should also invoke
> > String.valueOf(Object) method, which is the case when using JDK7. But it
> > calls String.valueOf(char[]) if using JDK8. Looks like this is the issue
> > that should be fixed. 
> > 
> > Moving back to JDT Core for analysis.
> 
> This is the expected behavior according to bug 487118.

Why is this not applicable to JDK7?

For the clean up issue, adding a breakpoint at compilationUnit.getProblems() in:

UnusedCodeFix.createCleanUp(CompilationUnit compilationUnit, boolean removeUnusedPrivateMethods, boolean removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields, boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables, boolean removeUnusedImports, boolean removeUnusedCast)

shows that CompilationUnit.getProblems() returns:
[Pb(181) Unnecessary cast from Object to Object]

which results in the clean up removing the cast.

CompilationUnit.getProblems() should not report the unnecessary cast problem in this case to fix this issue.
Comment 6 Stephan Herrmann CLA 2016-04-19 14:01:27 EDT
Sounds similar to the warning re redundant type arguments. In both cases the wrong warning enables a wrong quick fix.
Comment 7 Manoj N Palat CLA 2018-05-16 01:30:03 EDT
Bulk move out of 4.8
Comment 8 Eclipse Genie CLA 2020-07-02 02:51:53 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 9 Eclipse Genie CLA 2022-09-08 18:10:26 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 10 Srikanth Sankaran CLA 2023-12-28 02:37:31 EST
See https://bugs.eclipse.org/bugs/show_bug.cgi?id=573371