dominikh / go-tools

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

simple: S1009 doesn't handle `if x == nil || len(x) < N` redundant nil-checks #1605

Closed spencerschrock closed 1 month ago

spencerschrock commented 2 months ago

Code

package main

import (
        "os"
)

func main() {
        a := []string{}
        if a == nil || len(a) < 2 {
                os.Exit(1)
        }
        os.Exit(0)
}

Command

staticcheck -checks S1009 main.go

Expected

main.go:9:5: should omit nil check; len() for []string is defined as zero (S1009)

Got

No result. There's a comment that S1009 covers: https://github.com/dominikh/go-tools/blob/9f4b51e3ab5afa4a54784fc760186225c128355d/simple/s1009/s1009.go#L41-L48

I believe this can be expanded to include:

//  if x == nil || len(x) < N {} 
Version Info ``` staticcheck -version staticcheck 2024.1.1 (0.5.1) ``` ``` staticcheck -debug.version staticcheck 2024.1.1 (0.5.1) Compiled with Go version: go1.23.0 Main module: honnef.co/go/tools@v0.5.1 (sum: h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=) Dependencies: github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=) golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=) golang.org/x/mod@v0.17.0 (sum: h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=) golang.org/x/sync@v0.7.0 (sum: h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=) golang.org/x/tools@v0.21.1-0.20240531212143-b6235391adb3 (sum: h1:SHq4Rl+B7WvyM4XODon1LXtP7gcG49+7Jubt1gWWswY=) ``` ``` go version go version go1.23.0 linux/amd64 ``` ``` go env GO111MODULE='on' GOARCH='amd64' GOBIN='' GOCACHE='~/.cache/go-build' GOENV='~/.config/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='-mod=mod' GOHOSTARCH='amd64' GOHOSTOS='linux' GOINSECURE='' GOMODCACHE='~/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='linux' GOPATH='~/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='~/sdk/go1.23.0' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='~/sdk/go1.23.0/pkg/tool/linux_amd64' GOVCS='' GOVERSION='go1.23.0' GODEBUG='' GOTELEMETRY='local' GOTELEMETRYDIR='~/.config/go/telemetry' GCCGO='gccgo' GOAMD64='v1' AR='ar' CC='gcc' CXX='g++' CGO_ENABLED='1' GOMOD='/dev/null' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2627057867=/tmp/go-build -gno-record-gcc-switches' ```