Skip to content

Commit

Permalink
go/analysis: remove stray print statement in the timeformat analyzer
Browse files Browse the repository at this point in the history
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 <[email protected]>
gopls-CI: kokoro <[email protected]>
Run-TryBot: Robert Findley <[email protected]>
Reviewed-by: Tim King <[email protected]>
  • Loading branch information
findleyr committed Aug 12, 2022
1 parent 88d981e commit bebd890
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
2 changes: 0 additions & 2 deletions go/analysis/passes/timeformat/timeformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package timeformat

import (
"fmt"
"go/ast"
"go/constant"
"go/token"
Expand Down Expand Up @@ -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)

Expand Down
54 changes: 54 additions & 0 deletions gopls/internal/regtest/diagnostics/analysis_test.go
Original file line number Diff line number Diff line change
@@ -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"),
)
})
}
6 changes: 6 additions & 0 deletions internal/lsp/testdata/analyzer/bad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary.txt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 164
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 37
DiagnosticsCount = 38
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary_go1.18.txt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 174
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 37
DiagnosticsCount = 38
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
Expand Down

0 comments on commit bebd890

Please sign in to comment.