errcheck icon indicating copy to clipboard operation
errcheck copied to clipboard

Error must be checked before other code run

Open lx-world opened this issue 2 years ago • 1 comments


package main

import (
	"errors"
	"fmt"
	"os"
)

type Stu struct {
	Age int
}

func AAA() (*Stu, error) {
	return nil, errors.New("error here")
}

func BBB() (v int, err error) {
	stu, err := AAA() //lint must return if err != nil
	stu.Age = 1
	return
}

func main() {
	res, err := BBB()
	fmt.Fprintln(os.Stdout, res, err)
}

this code can't check the err not return, so it panic.

I hope that this situation can be warned, or whether it is turned on or not controlled by parameters

lx-world avatar Mar 29 '23 02:03 lx-world

Hm, there could very well be legitimate cases where one needs to run something before checking for error. For example, the one I had was where I wanted to issue a log statement regardless of whether error was nil or not.

dtcaciuc avatar Jul 03 '24 06:07 dtcaciuc

For now, closing due to inactivity from the OP

dtcaciuc avatar Dec 25 '24 18:12 dtcaciuc