go.lang.correctness.useless-eqeq.hardcoded-eq-true-or-false

Verifed by r2c
Community Favorite
profile photo of semgrepsemgrep
Author
105,427
Download Count*

Detected useless if statement. 'if (True)' and 'if (False)' always result in the same behavior, and therefore is not necessary in the code. Remove the 'if (False)' expression completely or just the 'if (True)' comparison depending on which expression is in the code.

Run Locally

Run in CI

Defintion

rules:
  - id: hardcoded-eq-true-or-false
    message: Detected useless if statement. 'if (True)' and 'if (False)' always
      result in the same behavior, and therefore is not necessary in the code.
      Remove the 'if (False)' expression completely or just the 'if (True)'
      comparison depending on which expression is in the code.
    languages:
      - go
    severity: INFO
    pattern-either:
      - pattern: if (true) { ... }
      - pattern: if (false) { ... }
    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")
    }
}