Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@spencerschrock

Description

@spencerschrock

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:

// run checks for the following redundant nil-checks:
//
// if x == nil || len(x) == 0 {}
// if x != nil && len(x) != 0 {}
// if x != nil && len(x) == N {} (where N != 0)
// if x != nil && len(x) > N {}
// if x != nil && len(x) >= N {} (where N != 0)
func run(pass *analysis.Pass) (interface{}, error) {

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/[email protected] (sum: h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=)
Dependencies:
	github.com/BurntSushi/[email protected] (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
	golang.org/x/exp/[email protected] (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
	golang.org/x/[email protected] (sum: h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=)
	golang.org/x/[email protected] (sum: h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=)
	golang.org/x/[email protected] (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'

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions