Go logoGo/
CRT-D0003

Possibly wrong conditional expressionCRT-D0003

Minor severityMinor
Bug Risk categoryBug Risk

Conditional expression might not be correct as a result of typos. It is recommended to careful vet the expression.

An idempotent condition, i.e., constantly evaluates to true or false for the possible values of the operands. This is usually a result of typos.

Suspicious conditional expressions should be properly vetted before running code in production.

Bad practice

for x := 0; x > 10; x++ {
    // body
}
if x < -10 && x > 10 { // always false
    // body
}
_ = x == 10 && x == 20 // x cannot be both i.e., 10 and 20
// Alternate #1:
for x := 0; x < 10; x++ {
    // body
}

// Alternate #2:
// If the condition is intended to always return `true` or `false`, the boolean
// values should be used directly.
for {
    // body
}
if -10 < x && x < 10 {
    // body
}
_ = x == 10 || x == 20