Skip to content

SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH not detected if loop follows dead store #3530

@csrma

Description

@csrma

Dead store due to switch fall through is not detected if a loop (for/while) follows the assignment operation, even though the dead store variable is not used in the loop. If other branching syntax (e.g., if-else) follows the assignment, the rule is detected correctly. It only misses if a loop follows.

Below is a reproducible example.

class DeadStoreDueToSwitchFallThrough {

    public String showBug(int value) {
        String valueType = null;
        switch (value % 2) {
            case 0:
                valueType = "even";        // this falls through and is over-written by valueType = "odd";
                for (int i = 0; i < 5; i++) {
                    System.out.println("Nice even value: " + i);
                }
            case 1:
                valueType = "odd";
                break;
            default:
                valueType = "invalid number";
        }
        return valueType;
    }
}

Expected Outcome: Error raised on block [5-16].

Spotbugs: 4.9.3
Java: Jdk 17 and Jdk 21
Run Method: CLI as:

javac DeadStoreDueToSwitchFallThrough.java && \
    java -jar $SPOTBUGS_JAR -textui -effort:max *.class

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions