go/analysis: remove stray print statement in the timeformat analyzer
A debugging print statement was left in the analyzer, which breaks
gopls' communication over stdin/stdout.
Fix this, and add tests.
Fixes golang/vscode-go#2406
Change-Id: I1b785fa09e66eae2f1b1e03806e5b59d2015e75e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/422902
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Tim King <taking@google.com>
diff --git a/go/analysis/passes/timeformat/timeformat.go b/go/analysis/passes/timeformat/timeformat.go
index 9147826..acb198f 100644
--- a/go/analysis/passes/timeformat/timeformat.go
+++ b/go/analysis/passes/timeformat/timeformat.go
@@ -7,7 +7,6 @@
package timeformat
import (
- "fmt"
"go/ast"
"go/constant"
"go/token"
@@ -59,7 +58,6 @@
if badAt > -1 {
// Check if it's a literal string, otherwise we can't suggest a fix.
if _, ok := arg.(*ast.BasicLit); ok {
- fmt.Printf("%#v\n", arg)
pos := int(arg.Pos()) + badAt + 1 // +1 to skip the " or `
end := pos + len(badFormat)
diff --git a/gopls/internal/regtest/diagnostics/analysis_test.go b/gopls/internal/regtest/diagnostics/analysis_test.go
new file mode 100644
index 0000000..fbebf60
--- /dev/null
+++ b/gopls/internal/regtest/diagnostics/analysis_test.go
@@ -0,0 +1,54 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package diagnostics
+
+import (
+ "testing"
+
+ "golang.org/x/tools/internal/lsp/protocol"
+ . "golang.org/x/tools/internal/lsp/regtest"
+)
+
+// Test for the timeformat analyzer, following golang/vscode-go#2406.
+//
+// This test checks that applying the suggested fix from the analyzer resolves
+// the diagnostic warning.
+func TestTimeFormatAnalyzer(t *testing.T) {
+ const files = `
+-- go.mod --
+module mod.com
+
+go 1.18
+-- main.go --
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+ now := time.Now()
+ fmt.Println(now.Format("2006-02-01"))
+}`
+
+ Run(t, files, func(t *testing.T, env *Env) {
+ env.OpenFile("main.go")
+
+ var d protocol.PublishDiagnosticsParams
+ env.Await(
+ OnceMet(
+ env.DoneWithOpen(),
+ env.DiagnosticAtRegexp("main.go", "2006-02-01"),
+ ReadDiagnostics("main.go", &d),
+ ),
+ )
+
+ env.ApplyQuickFixes("main.go", d.Diagnostics)
+ env.Await(
+ EmptyDiagnostics("main.go"),
+ )
+ })
+}
diff --git a/internal/lsp/testdata/analyzer/bad_test.go b/internal/lsp/testdata/analyzer/bad_test.go
index c819cbc..3d89101 100644
--- a/internal/lsp/testdata/analyzer/bad_test.go
+++ b/internal/lsp/testdata/analyzer/bad_test.go
@@ -4,6 +4,7 @@
"fmt"
"sync"
"testing"
+ "time"
)
func Testbad(t *testing.T) { //@diag("", "tests", "Testbad has malformed name: first letter after 'Test' must not be lowercase", "warning")
@@ -16,3 +17,8 @@
func printfWrapper(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
+
+func _() {
+ now := time.Now()
+ fmt.Println(now.Format("2006-02-01")) //@diag("2006-02-01", "timeformat", "2006-02-01 should be 2006-01-02", "warning")
+}
diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden
index b6c6c07..9324833 100644
--- a/internal/lsp/testdata/summary.txt.golden
+++ b/internal/lsp/testdata/summary.txt.golden
@@ -8,7 +8,7 @@
FuzzyCompletionsCount = 8
RankedCompletionsCount = 164
CaseSensitiveCompletionsCount = 4
-DiagnosticsCount = 37
+DiagnosticsCount = 38
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
diff --git a/internal/lsp/testdata/summary_go1.18.txt.golden b/internal/lsp/testdata/summary_go1.18.txt.golden
index 668d5fb..9ec3c79 100644
--- a/internal/lsp/testdata/summary_go1.18.txt.golden
+++ b/internal/lsp/testdata/summary_go1.18.txt.golden
@@ -8,7 +8,7 @@
FuzzyCompletionsCount = 8
RankedCompletionsCount = 174
CaseSensitiveCompletionsCount = 4
-DiagnosticsCount = 37
+DiagnosticsCount = 38
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8