Describe the bug
Tool fails to report Dm: Redundant invocation of toString() on a String when the toString() call is followed by another method in a chained expression, even though the toString() is redundant and unnecessary.
To Reproduce
public class RedundantToString {
public void test(String s) {
// --- TP (True Positive):
// Method().toString()
String tp = s.toUpperCase().toString(); //<-reported(True positive)
// --- FN (False Negative):
//Variable.toString().Method()
String fn = s.toString().toLowerCase();//<-should report(False Negative)
System.out.println(tp + fn);
}
}
Expected behavior
Both the chained calls s.toUpperCase().toString() and s.toString().toLowerCase() should be reported with Dm: invokes toString() method on a String.
Since both involve redundant toString() on a String object, they represent unnecessary method invocations that could be optimized.
Actual behavior
Only the call s.toUpperCase().toString() is reported. The call s.toString().toLowerCase() is silently ignored.
Environment
- Java version: JDK 21
- SpotBugs Version: 4.9.8
- How do you use SpotBugs: CLI
Describe the bug
Tool fails to report Dm: Redundant invocation of toString() on a String when the toString() call is followed by another method in a chained expression, even though the toString() is redundant and unnecessary.
To Reproduce
Expected behavior
Both the chained calls s.toUpperCase().toString() and s.toString().toLowerCase() should be reported with Dm: invokes toString() method on a String.
Since both involve redundant toString() on a String object, they represent unnecessary method invocations that could be optimized.
Actual behavior
Only the call s.toUpperCase().toString() is reported. The call s.toString().toLowerCase() is silently ignored.
Environment