Community
Participate
Working Groups
Build Identifier: 20110916-0149 After doing some method refactoring, I ended up with code that had toString() calls on a returning String. Before: String s = returnsUUID().toString(); After: String s = returnsString().toString(); That's redundant code. It would be nice to catch these kind of useless calls for String.toString() with a compiler warning option. Reproducible: Always
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.