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 <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/go/analysis/passes/defers/doc.go b/go/analysis/passes/defers/doc.go
index ec9f766..60ad3c2 100644
--- a/go/analysis/passes/defers/doc.go
+++ b/go/analysis/passes/defers/doc.go
@@ -21,5 +21,5 @@
 //
 // The correct code is:
 //
-//	defer func() { recordLatency(time.Since(start)) }()`
+//	defer func() { recordLatency(time.Since(start)) }()
 package defers
diff --git a/go/analysis/unitchecker/vet_std_test.go b/go/analysis/unitchecker/vet_std_test.go
index feea1a2..e0fb41c 100644
--- a/go/analysis/unitchecker/vet_std_test.go
+++ b/go/analysis/unitchecker/vet_std_test.go
@@ -19,6 +19,7 @@
 	"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"
@@ -54,6 +55,7 @@
 		cgocall.Analyzer,
 		composite.Analyzer,
 		copylock.Analyzer,
+		defers.Analyzer,
 		directive.Analyzer,
 		errorsas.Analyzer,
 		framepointer.Analyzer,
@@ -68,8 +70,8 @@
 		stdmethods.Analyzer,
 		stringintconv.Analyzer,
 		structtag.Analyzer,
-		tests.Analyzer,
 		testinggoroutine.Analyzer,
+		tests.Analyzer,
 		timeformat.Analyzer,
 		unmarshal.Analyzer,
 		unreachable.Analyzer,
diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md
index 8f470e2..48c98e0 100644
--- a/gopls/doc/analyzers.md
+++ b/gopls/doc/analyzers.md
@@ -108,6 +108,26 @@
 
 **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
diff --git a/gopls/doc/generate_test.go b/gopls/doc/generate_test.go
index 99f366c..44e6041 100644
--- a/gopls/doc/generate_test.go
+++ b/gopls/doc/generate_test.go
@@ -23,6 +23,6 @@
 		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")
 	}
 }
diff --git a/gopls/internal/lsp/source/api_json.go b/gopls/internal/lsp/source/api_json.go
index 153cb7d..97f6384 100644
--- a/gopls/internal/lsp/source/api_json.go
+++ b/gopls/internal/lsp/source/api_json.go
@@ -269,6 +269,11 @@
 							Default: "true",
 						},
 						{
+							Name:    "\"defer\"",
+							Doc:     "report common mistakes in defer statements\n\nThe defer analyzer reports a diagnostic when a defer statement would\nresult in a non-deferred call to time.Since, as experience has shown\nthat this is nearly always a mistake.\n\nFor example:\n\n\tstart := time.Now()\n\t...\n\tdefer recordLatency(time.Since(start)) // error: call to time.Since is not deferred\n\nThe correct code is:\n\n\tdefer func() { recordLatency(time.Since(start)) }()",
+							Default: "true",
+						},
+						{
 							Name:    "\"deprecated\"",
 							Doc:     "check for use of deprecated identifiers\n\nThe deprecated analyzer looks for deprecated symbols and package imports.\n\nSee https://go.dev/wiki/Deprecated to learn about Go's convention\nfor documenting and signaling deprecated identifiers.",
 							Default: "true",
@@ -967,6 +972,11 @@
 			Default: true,
 		},
 		{
+			Name:    "defer",
+			Doc:     "report common mistakes in defer statements\n\nThe defer analyzer reports a diagnostic when a defer statement would\nresult in a non-deferred call to time.Since, as experience has shown\nthat this is nearly always a mistake.\n\nFor example:\n\n\tstart := time.Now()\n\t...\n\tdefer recordLatency(time.Since(start)) // error: call to time.Since is not deferred\n\nThe correct code is:\n\n\tdefer func() { recordLatency(time.Since(start)) }()",
+			Default: true,
+		},
+		{
 			Name:    "deprecated",
 			Doc:     "check for use of deprecated identifiers\n\nThe deprecated analyzer looks for deprecated symbols and package imports.\n\nSee https://go.dev/wiki/Deprecated to learn about Go's convention\nfor documenting and signaling deprecated identifiers.",
 			Default: true,
diff --git a/gopls/internal/lsp/source/options.go b/gopls/internal/lsp/source/options.go
index 4c33014..c2e3223 100644
--- a/gopls/internal/lsp/source/options.go
+++ b/gopls/internal/lsp/source/options.go
@@ -26,6 +26,7 @@
 	"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"
@@ -1544,6 +1545,7 @@
 		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},