Skip to content

Commit

Permalink
gopls/internal/lsp/source: enable new defers analyzer
Browse files Browse the repository at this point in the history
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 <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
adonovan authored and gopherbot committed Aug 7, 2023
1 parent 2dc7eba commit 229f848
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go/analysis/passes/defers/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
//
// The correct code is:
//
// defer func() { recordLatency(time.Since(start)) }()`
// defer func() { recordLatency(time.Since(start)) }()
package defers
4 changes: 3 additions & 1 deletion go/analysis/unitchecker/vet_std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"golang.org/x/tools/go/analysis/passes/cgocall"
"golang.org/x/tools/go/analysis/passes/composite"
"golang.org/x/tools/go/analysis/passes/copylock"
"golang.org/x/tools/go/analysis/passes/defers"
"golang.org/x/tools/go/analysis/passes/directive"
"golang.org/x/tools/go/analysis/passes/errorsas"
"golang.org/x/tools/go/analysis/passes/framepointer"
Expand Down Expand Up @@ -54,6 +55,7 @@ func vet() {
cgocall.Analyzer,
composite.Analyzer,
copylock.Analyzer,
defers.Analyzer,
directive.Analyzer,
errorsas.Analyzer,
framepointer.Analyzer,
Expand All @@ -68,8 +70,8 @@ func vet() {
stdmethods.Analyzer,
stringintconv.Analyzer,
structtag.Analyzer,
tests.Analyzer,
testinggoroutine.Analyzer,
tests.Analyzer,
timeformat.Analyzer,
unmarshal.Analyzer,
unreachable.Analyzer,
Expand Down
20 changes: 20 additions & 0 deletions gopls/doc/analyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ errors is discouraged.

**Enabled by default.**

## **defer**

report common mistakes in defer statements

The defer analyzer reports a diagnostic when a defer statement would
result in a non-deferred call to time.Since, as experience has shown
that this is nearly always a mistake.

For example:

start := time.Now()
...
defer recordLatency(time.Since(start)) // error: call to time.Since is not deferred

The correct code is:

defer func() { recordLatency(time.Since(start)) }()

**Enabled by default.**

## **deprecated**

check for use of deprecated identifiers
Expand Down
2 changes: 1 addition & 1 deletion gopls/doc/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func TestGenerated(t *testing.T) {
t.Fatal(err)
}
if !ok {
t.Error("documentation needs updating. run: `go run doc/generate.go` from the gopls module.")
t.Error("documentation needs updating. Run: cd gopls && go generate ./doc")
}
}
10 changes: 10 additions & 0 deletions gopls/internal/lsp/source/api_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gopls/internal/lsp/source/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"golang.org/x/tools/go/analysis/passes/composite"
"golang.org/x/tools/go/analysis/passes/copylock"
"golang.org/x/tools/go/analysis/passes/deepequalerrors"
"golang.org/x/tools/go/analysis/passes/defers"
"golang.org/x/tools/go/analysis/passes/directive"
"golang.org/x/tools/go/analysis/passes/errorsas"
"golang.org/x/tools/go/analysis/passes/fieldalignment"
Expand Down Expand Up @@ -1544,6 +1545,7 @@ func defaultAnalyzers() map[string]*Analyzer {
cgocall.Analyzer.Name: {Analyzer: cgocall.Analyzer, Enabled: true},
composite.Analyzer.Name: {Analyzer: composite.Analyzer, Enabled: true},
copylock.Analyzer.Name: {Analyzer: copylock.Analyzer, Enabled: true},
defers.Analyzer.Name: {Analyzer: defers.Analyzer, Enabled: true},
deprecated.Analyzer.Name: {Analyzer: deprecated.Analyzer, Enabled: true, Severity: protocol.SeverityHint, Tag: []protocol.DiagnosticTag{protocol.Deprecated}},
directive.Analyzer.Name: {Analyzer: directive.Analyzer, Enabled: true},
errorsas.Analyzer.Name: {Analyzer: errorsas.Analyzer, Enabled: true},
Expand Down

0 comments on commit 229f848

Please sign in to comment.