8000 SA4017 false positive · Issue #949 · dominikh/go-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
SA4017 false positive #949
Closed
Closed
@arashpayan

Description

@arashpayan

Firstly, thank you for building such an incredible tool! It's saved me from many bugs. :-)

I recently encountered a false positive while working on some code that I've reduced to the smallest reproducible example I could and attached to this issue.

  • The output of 'staticcheck -version'
    staticcheck 2020.2.3 (v0.1.3)

  • The output of 'staticcheck -debug.version' (it is fine if this command fails)

staticcheck 2020.2.3 (v0.1.3)

Compiled with Go version: go1.16.2
Main module:
	honnef.co/go/tools@v0.1.3 (sum: h1:qTakTkI6ni6LFD5sBwwsdSO+AQqbSIxOauHTTQKZ/7o=)
Dependencies:
	github.com/BurntSushi/toml@v0.3.1 (sum: h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=)
	golang.org/x/mod@v0.3.0 (sum: h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=)
	golang.org/x/sys@v0.0.0-20210119212857-b64e53b001e4 (sum: h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=)
	golang.org/x/tools@v0.1.0 (sum: h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=)
	golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 (sum: h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=)
  • The output of 'go version'
    go version go1.16.2 linux/amd64

  • The output of 'go env'

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/<REDACTED>/.cache/go-build"
GOENV="/home/<REDACTED>/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/<REDACTED>/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/<REDACTED>/gocode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/<REDACTED>/scheck/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build199834044=/tmp/go-build -gno-record-gcc-switches"
  • Exactly which command you ran
    staticcheck ./...

  • Output of the command and what's wrong with the output
    app/app.go:21:10: Errorf is a pure function but its return value is ignored (SA4017)
    The return value from fmt.Errorf is used as the return value of the function. It's definitely being used.

  • Where we can read the code you're running staticcheck on
    I've attached a tiny project that reproduces the bug with the smallest example I could make. scheck.tar.gz

There seem to be 2 key elements to triggering the bug.

  1. The method must not be in the main package.
  2. If I remove the for loop at the end of the function, the error also disappears.

The example is based on a server that I'm writing, hence the non-terminating for loop.

In short, staticcheck has a problem with the following bit of code:

func Run() error {
	err := configEnvironment()
	if err != nil {
		return fmt.Errorf("failed to load config: %v", err)  // <-- FALSE POSITIVE HERE
	}
        // method continues

If I return err directly instead of wrapping it with fmt.Errorf() the error will also go away.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0