Skip to content

ASE_ASSERTION_WITH_SIDE_EFFECT_METHOD not detected when incrementing class field #3463

@csrma

Description

@csrma

The following code increments the field counter in an assertion statement. This should be detected by SpotBugs as "side effects in assertion" but it is missed.

class AssertionWithSideEffect {
    private int counter = 0;

    public int showBug() {
        assert ++counter > 0;   // side effect in assertion
        return counter;
    }
}

SpotBugs version: 4.9.2
Java: Temurin openjdk 21.0.7
Ran from CLI as the following two commands:

export SPOTBUGS_JAR=/path/to/spotbugs.jar
javac AssertionWithSideEffect.java && \
java -jar $SPOTBUGS_JAR \
    -low \
    -effort:max \
    AssertionWithSideEffect.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