errcheck icon indicating copy to clipboard operation
errcheck copied to clipboard

How to exclude builtin functions?

Open Antonboom opened this issue 2 years ago • 2 comments

Hello!

# .golangci.yml
linters-settings:
  errcheck:
    exclude-functions:
      - recover
$ cat example.go
package main

func main() {
	defer func() {
		recover()
	}()
}
$ golangci-lint run example.go
example.go:5:10: Error return value is not checked (errcheck)
		recover()

Antonboom avatar Mar 22 '22 14:03 Antonboom

Hello,

Right now excluding recover() is not possible. I personally think that recover() failure is something you want to handle one way or another but, if you really want to ignore it, the simplest thing right now would be to use _ = assignment.

dtcaciuc avatar Mar 22 '22 16:03 dtcaciuc

Sometimes it's ok to ignore recover() without explicit _ and it doesn't matter since recover() is just a special case of a more general case.

Some examples from std:

func() {
	defer func() { recover() }()
	if m.Call(nil)[0].Bool() {
		name := strings.TrimSuffix(tm.Name, "_")
		fmt.Fprintf(w, " %s", name)
	}
}()
func interfaceEqual(a, b interface{}) bool {
	defer func() {
		recover()
	}()
	return a == b
}
t.Run(fmt.Sprintf("%T(%v)", tc, tc), func(t *testing.T) {
	defer func() {
		recover()
	}()
	if errors.As(err, tc) {
		t.Errorf("As(err, %T(%v)) = true, want false", tc, tc)
		return
	}
	t.Errorf("As(err, %T(%v)) did not panic", tc, tc)
})

If you see no reason to support specifying any functions to exclude - OK

Antonboom avatar Mar 22 '22 17:03 Antonboom