Skip to content

Commit 44f7796

Browse files
findleyrgopherbot
authored andcommitted
gopls: add and enable the slog analyzer
This analyzer is included in go/vet, and so gopls should have it as well. Change-Id: Ib5cbee44a1f38c4aa45d75bcaa7a345d88099d8e Reviewed-on: https://go-review.googlesource.com/c/tools/+/524764 Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 2c6ba93 commit 44f7796

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

gopls/doc/analyzers.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,24 @@ This is one of the simplifications that "gofmt -s" applies.
494494

495495
**Enabled by default.**
496496

497+
## **slog**
498+
499+
check for invalid structured logging calls
500+
501+
The slog checker looks for calls to functions from the log/slog
502+
package that take alternating key-value pairs. It reports calls
503+
where an argument in a key position is neither a string nor a
504+
slog.Attr, and where a final key is missing its value.
505+
For example,it would report
506+
507+
slog.Warn("message", 11, "k") // slog.Warn arg "11" should be a string or a slog.Attr
508+
509+
and
510+
511+
slog.Info("message", "k1", v1, "k2") // call to slog.Info missing a final value
512+
513+
**Enabled by default.**
514+
497515
## **sortslice**
498516

499517
check the argument type of sort.Slice

gopls/internal/lsp/source/api_json.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/lsp/source/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"golang.org/x/tools/go/analysis/passes/printf"
4040
"golang.org/x/tools/go/analysis/passes/shadow"
4141
"golang.org/x/tools/go/analysis/passes/shift"
42+
"golang.org/x/tools/go/analysis/passes/slog"
4243
"golang.org/x/tools/go/analysis/passes/sortslice"
4344
"golang.org/x/tools/go/analysis/passes/stdmethods"
4445
"golang.org/x/tools/go/analysis/passes/stringintconv"
@@ -1549,6 +1550,7 @@ func defaultAnalyzers() map[string]*Analyzer {
15491550
nilfunc.Analyzer.Name: {Analyzer: nilfunc.Analyzer, Enabled: true},
15501551
printf.Analyzer.Name: {Analyzer: printf.Analyzer, Enabled: true},
15511552
shift.Analyzer.Name: {Analyzer: shift.Analyzer, Enabled: true},
1553+
slog.Analyzer.Name: {Analyzer: slog.Analyzer, Enabled: true},
15521554
stdmethods.Analyzer.Name: {Analyzer: stdmethods.Analyzer, Enabled: true},
15531555
stringintconv.Analyzer.Name: {Analyzer: stringintconv.Analyzer, Enabled: true},
15541556
structtag.Analyzer.Name: {Analyzer: structtag.Analyzer, Enabled: true},
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
11
Test of warning diagnostics from various analyzers:
2-
tests, copylocks, printf, and timeformat.
2+
copylocks, printf, slog, tests, and timeformat.
33

44
-- go.mod --
55
module example.com
66
go 1.12
77

8+
-- flags --
9+
-min_go=go1.21
10+
811
-- bad_test.go --
912
package analyzer
1013

1114
import (
1215
"fmt"
16+
"log/slog"
1317
"sync"
1418
"testing"
1519
"time"
1620
)
1721

18-
func Testbad(t *testing.T) { //@diag("", re"Testbad has malformed name: first letter after 'Test' must not be lowercase")
22+
// copylocks
23+
func _() {
1924
var x sync.Mutex
2025
_ = x //@diag("x", re"assignment copies lock value to _: sync.Mutex")
26+
}
2127

28+
// printf
29+
func _() {
2230
printfWrapper("%s") //@diag(re`printfWrapper\(.*\)`, re"example.com.printfWrapper format %s reads arg #1, but call has 0 args")
2331
}
2432

2533
func printfWrapper(format string, args ...interface{}) {
2634
fmt.Printf(format, args...)
2735
}
2836

37+
// slog
38+
func _() {
39+
slog.Info("msg", 1) //@diag("1", re`slog.Info arg "1" should be a string or a slog.Attr`)
40+
}
41+
42+
// tests
43+
func Testbad(t *testing.T) { //@diag("", re"Testbad has malformed name: first letter after 'Test' must not be lowercase")
44+
}
45+
46+
// timeformat
2947
func _() {
3048
now := time.Now()
3149
fmt.Println(now.Format("2006-02-01")) //@diag("2006-02-01", re"2006-02-01 should be 2006-01-02")
3250
}
51+

0 commit comments

Comments
 (0)