| Summary: | New warning to detect unnecessary toString() on String | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Paul Benedict <pbenedict> |
| Component: | Core | Assignee: | Ayushman Jain <amj87.iitr> |
| Status: | NEW --- | QA Contact: | |
| Severity: | enhancement | ||
| Priority: | P3 | CC: | amj87.iitr, avijayr, deepakazad, ulrich.everling |
| Version: | 3.8 | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Windows 7 | ||
| Whiteboard: | |||
|
Description
Paul Benedict
This is essentially a search for method references and hence I would rather fix bug 209145 as that would be a more general solution for all methods. Also note that a call to String#toString() is harmless as the method is defined as..
public String toString() {
return this;
}
If bug 209145 can be fixed, perhaps then Clean Up could be enhanced to remove these spurious invocations. This is independent of bug #209145 because String is final so toString() cannot be overridden. The warning should be raised for x.toString() whenever x is an expression of type java.lang.String The Quick Fix should remove the method call, leaving x alone. Of course x is not entirely equivalent with x.toString() because, when x happens to evaluate to null, x.toString() throws a NullPointerException. *** Bug 44787 has been marked as a duplicate of this bug. *** When x is an expression of type String, and x.toString() is used as a StatementExpression, then x alone may or may not be suitable as a StatementExpression. If not, the Quick Fix of omitting the method call and keeping x is inappropriate; instead, a quick fix should consider omitting x as well but keeping side effects if any. The warning itself makes sense anyway. |