java.lang.correctness.eqeq.eqeq

Verifed by r2c
Community Favorite
profile photo of returntocorpreturntocorp
Author
84,383
Download Count*

$X == $X or $X != $X is always true. (Unless the value compared is a float or double). To test if $X is not-a-number, use Double.isNaN($X).

Run Locally

Run in CI

Defintion

rules:
  - id: eqeq
    patterns:
      - pattern-not-inside: assert $X;
      - pattern-not-inside: |
          assert $X : $Y;
      - pattern-either:
          - pattern: $X == $X
          - pattern: $X != $X
      - pattern-not: 1 == 1
    message: "`$X == $X` or `$X != $X` is always true. (Unless the value compared is
      a float or double). To test if `$X` is not-a-number, use
      `Double.isNaN($X)`."
    languages:
      - java
    severity: ERROR
    metadata:
      category: correctness
      technology:
        - java
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

eqeq.java

class Bar {
    void main() {
        boolean myBoolean;

        //myBoolean == myBoolean;

        // ruleid:eqeq
        if (myBoolean == myBoolean) {
            continue;
        }

        // ruleid:eqeq
        if (myBoolean != myBoolean) {
            continue;
        }

        float someFloat;
        // ruleid:eqeq
        if (someFloat != someFloat) {
            continue;
        }
    }
}