Pattern: Nil
function comparison
Issue: -
Comparisons between functions and nil
result in redundant (always true
) or unreachable (always false
) code.
Redundant code may increase maintenance costs by making code more difficult to change and to understand. Remove such code to resolve this issue.
Unreachable code is generally considered undesirable for a number of reasons, including increased code size and increased time and effort for maintenance. Update or remove such code to resolve this issue.
Example of incorrect code:
func F() {}
func Comparison() {
if F == nil { // comparison of function F == nil is always false
panic("can't happen")
}
}
Example of correct code:
func F() {}
var Fv = F
func Comparison() {
if Fv == nil {
// no error; func vars or fields may be nil
}
}