Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S1008 should know that builtins like len and cap never return negative values #1422

Closed
mvdan opened this issue Jul 9, 2023 · 0 comments · Fixed by #1423
Closed

S1008 should know that builtins like len and cap never return negative values #1422

mvdan opened this issue Jul 9, 2023 · 0 comments · Fixed by #1423
Labels
bug needs-triage Newly filed issue that needs triage

Comments

@mvdan
Copy link
Contributor

mvdan commented Jul 9, 2023

$ staticcheck -debug.version
staticcheck (devel, v0.5.0-0.dev.0.20230706211743-ddee6bbaa341)

Compiled with Go version: devel go1.21-5c15498609 Fri Jul 7 22:02:26 2023 +0000
Main module:
	honnef.co/go/tools@v0.5.0-0.dev.0.20230706211743-ddee6bbaa341 (sum: h1:jNlTAPEjbDiN9qda/1wple0GSpewFnWhvc1GO7bZX1U=)
Dependencies:
	github.com/BurntSushi/toml@v1.2.1 (sum: h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=)
	golang.org/x/exp/typeparams@v0.0.0-20221208152030-732eee02a75a (sum: h1:Jw5wfR+h9mnIYH+OtGT2im5wV1YGGDora5vTv/aa5bE=)
	golang.org/x/mod@v0.10.0 (sum: h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=)
	golang.org/x/sys@v0.8.0 (sum: h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=)
	golang.org/x/tools@v0.9.4-0.20230601214343-86c93e8732cc (sum: h1:mqZawFxUzsv+YVwGQO30cZegeV/YD6dAwsdGxi0tQQg=)
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/mvdan/.cache/go-build'
GOENV='/home/mvdan/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/mvdan/go/pkg/mod'
GONOPROXY='github.com/cue-unity'
GONOSUMDB='github.com/cue-unity'
GOOS='linux'
GOPATH='/home/mvdan/go'
GOPRIVATE='github.com/cue-unity'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/mvdan/tip'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/mvdan/tip/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.21-5c15498609 Fri Jul 7 22:02:26 2023 +0000'
GCCGO='gccgo'
GOAMD64='v3'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/mvdan/src/voc-node/go.mod'
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-build75460387=/tmp/go-build -gno-record-gcc-switches'

As discussed in Slack, I got a suggestion like:

should use 'return len(t) <= 0' instead of 'if len(t) > 0 { return false }; return true' (S1008)

Most Go developers would write len(t) == 0 instead of len(t) <= 0, since we know that builtins like len or cap cannot return negative values. Staticcheck should know the same.

@mvdan mvdan added bug needs-triage Newly filed issue that needs triage labels Jul 9, 2023
mvdan added a commit to mvdan/go-tools that referenced this issue Jul 9, 2023
Rather than "len(x) <= 0", which is technically correct,
but not actually what most Go programmers would write,
since funcs like len or cap can never return negative integers.

Do the same for cap and copy, which never return negative ints either.

Fixes dominikh#1422.
dominikh pushed a commit that referenced this issue Jul 9, 2023
Rather than "len(x) <= 0", which is technically correct,
but not actually what most Go programmers would write,
since funcs like len or cap can never return negative integers.

Do the same for cap and copy, which never return negative ints either.

Fixes #1422.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-triage Newly filed issue that needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant