go.lang.correctness.useless-eqeq.eqeq-is-bad

profile photo of returntocorpreturntocorp
Author
4,989
Download Count*

Detected useless comparison operation $X == $X or $X != $X. This will always return 'True' or 'False' and therefore is not necessary. Instead, remove this comparison operation or use another comparison expression that is not deterministic.

Run Locally

Run in CI

Defintion

rules:
  - id: eqeq-is-bad
    patterns:
      - pattern-not-inside: assert(...)
      - pattern-either:
          - pattern: $X == $X
          - pattern: $X != $X
      - pattern-not: 1 == 1
    message: Detected useless comparison operation `$X == $X` or `$X != $X`. This
      will always return 'True' or 'False' and therefore is not necessary.
      Instead, remove this comparison operation or use another comparison
      expression that is not deterministic.
    languages:
      - go
    severity: ERROR
    metadata:
      category: correctness
      technology:
        - go
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

useless-eqeq.go

package main
import "fmt"

func main() {
    fmt.Println("hello world")
    var y = "hello";
    // ruleid:eqeq-is-bad
    fmt.Println(y == y)
    // ok:eqeq-is-bad
    assert(y == y)

    // ruleid:hardcoded-eq-true-or-false
    if (false) {
        fmt.Println("never")
    }
}