Skip to content

False negative with defer and golang.org/x/tools v0.22.0+ #19

@maratori

Description

@maratori

We use the nilerr as part of golangci-lint. It uses a newer version of golang.org/x/tools library, producing false negative results. The problem appears starting from golang.org/x/tools v0.22.0. Now, you use an outdated version of the library:

golang.org/x/tools v0.0.0-20201023174141-c8cfbd0f21e6

Here is the minimal test that passes with golang.org/x/tools v0.21.0 and fails with golang.org/x/tools v0.22.0 (or greater):

package a

func issue() error {
	defer func() {}()

	err := do()
	if err != nil {
		return nil // want "error is not nil \\(line 6\\) but it returns nil"
	}

	return nil
}
$ go get golang.org/x/tools@v0.21.0
go: upgraded golang.org/x/tools v0.0.0-20201023174141-c8cfbd0f21e6 => v0.21.0
$ go mod tidy
$ go test    
PASS
ok      github.com/gostaticanalysis/nilerr      0.391s
$ go get golang.org/x/tools@v0.22.0
go: upgraded golang.org/x/tools v0.21.0 => v0.22.0
$ go mod tidy
$ go test                          
--- FAIL: Test (0.57s)
    analysistest.go:604: a/a.go:192: no diagnostic was reported matching `error is not nil \(lines \[178 181\]\) but it returns nil`
    analysistest.go:604: a/b.go:8: no diagnostic was reported matching `error is not nil \(line 6\) but it returns nil`
FAIL
exit status 1
FAIL    github.com/gostaticanalysis/nilerr      0.778s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions