-
Notifications
You must be signed in to change notification settings - Fork 9
Closed
Description
As a minimal test case, we can use main.go
:
package main
import (
"log/slog"
)
const thisFileConstMsg = "Hello from main.go"
const localCopy = otherFileConstMsg
func main() {
slog.Info(thisFileConstMsg)
slog.Info(otherFileConstMsg)
slog.Info(localCopy)
}
and message.go
, in the same directory as main.go
:
package main
const otherFileConstMsg = "Hello from message.go"
If I go run
the directory, I get the expected three log lines:
$ go run .
2025/07/09 14:28:15 INFO Hello from main.go
2025/07/09 14:28:15 INFO Hello from message.go
2025/07/09 14:28:15 INFO Hello from message.go
But if I try to analyze the file:
$ sloglint -static-msg=true ./_test/
.../main.go:13:12: message should be a string literal or a constant
exit status 3
If I comment or remove the slog.Info(otherFileConstMsg)
, analysis succeeds.
From a quick inspection of sloglint source, I see that in isStaticMsg
:
case *ast.Ident: // e.g. const msg = "msg"; slog.Info(msg)
return msg.Obj != nil && msg.Obj.Kind == ast.Con
we observe msg.Obj.Kind
to be nil when the analysis fails.
I've observed this on the current main commit (0a750b1) and in tag v0.11.0 which is bundled in the current release of golang-ci.
tmzane
Metadata
Metadata
Assignees
Labels
No labels