Skip to content

Commit 229f848

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/lsp/source: enable new defers analyzer
This change enables the new defers analyzer in gopls. It also adds it to the vet compatibility test. A follow-up change will add it to vet itself. Also, remove stray backquote in doc comment. Updates golang/go#60048 Change-Id: I42f09bb79fcbe4e48593dd32fd066ddd39b9626f Reviewed-on: https://go-review.googlesource.com/c/tools/+/502975 Run-TryBot: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 2dc7eba commit 229f848

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

go/analysis/passes/defers/doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
//
2222
// The correct code is:
2323
//
24-
// defer func() { recordLatency(time.Since(start)) }()`
24+
// defer func() { recordLatency(time.Since(start)) }()
2525
package defers

go/analysis/unitchecker/vet_std_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/tools/go/analysis/passes/cgocall"
2020
"golang.org/x/tools/go/analysis/passes/composite"
2121
"golang.org/x/tools/go/analysis/passes/copylock"
22+
"golang.org/x/tools/go/analysis/passes/defers"
2223
"golang.org/x/tools/go/analysis/passes/directive"
2324
"golang.org/x/tools/go/analysis/passes/errorsas"
2425
"golang.org/x/tools/go/analysis/passes/framepointer"
@@ -54,6 +55,7 @@ func vet() {
5455
cgocall.Analyzer,
5556
composite.Analyzer,
5657
copylock.Analyzer,
58+
defers.Analyzer,
5759
directive.Analyzer,
5860
errorsas.Analyzer,
5961
framepointer.Analyzer,
@@ -68,8 +70,8 @@ func vet() {
6870
stdmethods.Analyzer,
6971
stringintconv.Analyzer,
7072
structtag.Analyzer,
71-
tests.Analyzer,
7273
testinggoroutine.Analyzer,
74+
tests.Analyzer,
7375
timeformat.Analyzer,
7476
unmarshal.Analyzer,
7577
unreachable.Analyzer,

gopls/doc/analyzers.md

+20
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ errors is discouraged.
108108

109109
**Enabled by default.**
110110

111+
## **defer**
112+
113+
report common mistakes in defer statements
114+
115+
The defer analyzer reports a diagnostic when a defer statement would
116+
result in a non-deferred call to time.Since, as experience has shown
117+
that this is nearly always a mistake.
118+
119+
For example:
120+
121+
start := time.Now()
122+
...
123+
defer recordLatency(time.Since(start)) // error: call to time.Since is not deferred
124+
125+
The correct code is:
126+
127+
defer func() { recordLatency(time.Since(start)) }()
128+
129+
**Enabled by default.**
130+
111131
## **deprecated**
112132

113133
check for use of deprecated identifiers

gopls/doc/generate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ func TestGenerated(t *testing.T) {
2323
t.Fatal(err)
2424
}
2525
if !ok {
26-
t.Error("documentation needs updating. run: `go run doc/generate.go` from the gopls module.")
26+
t.Error("documentation needs updating. Run: cd gopls && go generate ./doc")
2727
}
2828
}

gopls/internal/lsp/source/api_json.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/lsp/source/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"golang.org/x/tools/go/analysis/passes/composite"
2727
"golang.org/x/tools/go/analysis/passes/copylock"
2828
"golang.org/x/tools/go/analysis/passes/deepequalerrors"
29+
"golang.org/x/tools/go/analysis/passes/defers"
2930
"golang.org/x/tools/go/analysis/passes/directive"
3031
"golang.org/x/tools/go/analysis/passes/errorsas"
3132
"golang.org/x/tools/go/analysis/passes/fieldalignment"
@@ -1544,6 +1545,7 @@ func defaultAnalyzers() map[string]*Analyzer {
15441545
cgocall.Analyzer.Name: {Analyzer: cgocall.Analyzer, Enabled: true},
15451546
composite.Analyzer.Name: {Analyzer: composite.Analyzer, Enabled: true},
15461547
copylock.Analyzer.Name: {Analyzer: copylock.Analyzer, Enabled: true},
1548+
defers.Analyzer.Name: {Analyzer: defers.Analyzer, Enabled: true},
15471549
deprecated.Analyzer.Name: {Analyzer: deprecated.Analyzer, Enabled: true, Severity: protocol.SeverityHint, Tag: []protocol.DiagnosticTag{protocol.Deprecated}},
15481550
directive.Analyzer.Name: {Analyzer: directive.Analyzer, Enabled: true},
15491551
errorsas.Analyzer.Name: {Analyzer: errorsas.Analyzer, Enabled: true},

0 commit comments

Comments
 (0)