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

x/tools/go/analysis/passes/slog: False negitive when error type used as key. #65740

Open
aDotInTheVoid opened this issue Feb 16, 2024 · 1 comment
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@aDotInTheVoid
Copy link

Go version

go version go1.22.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/alona/.cache/go-build'
GOENV='/home/alona/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/alona/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/alona/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/alona/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.linux-amd64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/alona/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.linux-amd64/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/tmp/goo/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-build3366501523=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I wrote code that assumed slog methods worked like fmt.Printf, instead of talking Attr/key-value pairs:

package main

import "log/slog"

func main() {
        var x error
        slog.Error("The error is %v", x)
}

playground

Running go vet -slog ./main.go

What did you see happen?

go vet -slog reports no errors, but the key isn't present.

$ go vet -slog ./main.go
$ go run ./main.go
2024/02/16 00:50:48 ERROR The error is %v !BADKEY=<nil>

What did you expect to see?

go vet -slog ./main.go should report an error like ./main.go:7:32: slog.Error arg "x" should be a string or a slog.Attr (possible missing key or value).

Interestingly, if you change the type of x to int, the error fires as you'd expect.

package main

import "log/slog"

func main() {
        var x int
        slog.Error("The error is %v", x)
}
$ go vet -slog ./main.go
# command-line-arguments
# [command-line-arguments]
./main.go:7:32: slog.Error arg "x" should be a string or a slog.Attr (possible missing key or value)
$ go run ./main.go
2024/02/16 00:52:58 ERROR The error is %v !BADKEY=0
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Feb 16, 2024
@gopherbot gopherbot added this to the Unreleased milestone Feb 16, 2024
@seankhliao seankhliao changed the title x/tools/go/analysis/passes/slog/: False negitive when error type used as key. x/tools/go/analysis/passes/slog: False negitive when error type used as key. Feb 16, 2024
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 16, 2024
@thanm
Copy link
Contributor

thanm commented Feb 16, 2024

@jba per owners

@jba jba self-assigned this Feb 16, 2024
philip-peterson pushed a commit to philip-peterson/go that referenced this issue Apr 12, 2024
Previously, if we saw an arg of interface type passed to a slog logging
function, we made no assumptions about it.

But we can do better when the argument is in key position:
- If neither string nor slog.Attr can implement the interface,
  we can immediately report an error.
- If only slog.Attr can implement the interface, then we can
  assume that the next position is a key and carry on.

Fixes golang#65740.

Change-Id: I1080c26b6cb46a46489f811e4cd73c9e505ce6b3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/566895
Reviewed-by: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants