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>
This commit is contained in:
Robert Findley 2022-08-12 13:01:36 -04:00
parent 88d981ef8f
commit bebd890374
5 changed files with 62 additions and 4 deletions

View File

@ -7,7 +7,6 @@
package timeformat
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
@ -59,7 +58,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
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)

View File

@ -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"),
)
})
}

View File

@ -4,6 +4,7 @@ import (
"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 Testbad(t *testing.T) { //@diag("", "tests", "Testbad has malformed name: f
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")
}

View File

@ -8,7 +8,7 @@ DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 164
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 37
DiagnosticsCount = 38
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8

View File

@ -8,7 +8,7 @@ DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 174
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 37
DiagnosticsCount = 38
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8