dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.21k stars 377 forks source link

"potential nil panic" where it's not possible #1607

Closed kevinburkesegment closed 1 month ago

kevinburkesegment commented 1 month ago

I ran

go run honnef.co/go/tools/cmd/staticcheck@latest ./...

This complained about the following code:

                                 argmap := make(map[string]bool)
                if strings.Contains(arg, "=") {
                    parts := strings.SplitN(arg, "=", 2)
                    argmap[parts[0]] = true
                }

The error I got was as follows:

/var/lib/buildkite-agent/builds/buildkite-ci-076791110486c8a56/segment/lokicli/cmd/lokicli/lokicli.go:100:13: error: Potential nil panic detected. Observed nil flow from source to dereference point:
    - strings/strings.go:238:10: literal `nil` returned from `genSplit()` in position 0
    - strings/strings.go:278:53: result 0 of `genSplit()` returned from `SplitN()` in position 0
    - lokicli/lokicli.go:100:13: result 0 of `SplitN()` sliced into via the assignment(s):
        - `strings.SplitN(...)` to `parts` at lokicli/lokicli.go:99:6

I don't think that this can nil panic because of the Contains line above. I guess one thing I could do is just try the split and check for nil, but this obscures the intention of the code more.

dominikh commented 1 month ago

That diagnostic is from https://github.com/uber-go/nilaway, not Staticcheck.