kisielk / errcheck

errcheck checks that you checked errors.
MIT License
2.36k stars 137 forks source link

Error must be checked before other code run #232

Open lx-world opened 1 year ago

lx-world commented 1 year ago

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

dtcaciuc commented 4 months ago

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.