Skip to content

False Negative: Dm missing for toString() followed by method call in chained expressions #3966

Description

@leemeii

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions