Go

Go

Made by DeepSource

Unchecked error in if statement GO-W4016

Bug risk
Minor

if statements can be preceded with a simple statement where error is assigned to a variable which is not later checked in the boolean experession check which decides which expression block to execute. It is recommended to vet such cases as the results might not be as intended.

For example,

if err := expr(); err2 != nil {
    // TODO: Do something
}

Here, err is being assigned where expr() returns an err but we are checking for err2 later instead of err which seems suspicious.

Examples

Bad practice

if err := expr(); err2 != nil { // err2 is checked instead of err
    // TODO: Do something
}

Recommended

if err := expr(); err != nil {
    // TODO: Do something
}