diff --git a/go/analysis/passes/timeformat/timeformat.go b/go/analysis/passes/timeformat/timeformat.go index 9147826b4a..acb198f95c 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 @@ 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) diff --git a/gopls/internal/regtest/diagnostics/analysis_test.go b/gopls/internal/regtest/diagnostics/analysis_test.go new file mode 100644 index 0000000000..fbebf602dc --- /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 c819cbc011..3d89101fc0 100644 --- a/internal/lsp/testdata/analyzer/bad_test.go +++ b/internal/lsp/testdata/analyzer/bad_test.go @@ -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") +} diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index b6c6c07b15..9324833570 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -8,7 +8,7 @@ DeepCompletionsCount = 5 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 668d5fb82e..9ec3c79d3c 100644 --- a/internal/lsp/testdata/summary_go1.18.txt.golden +++ b/internal/lsp/testdata/summary_go1.18.txt.golden @@ -8,7 +8,7 @@ DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 RankedCompletionsCount = 174 CaseSensitiveCompletionsCount = 4 -DiagnosticsCount = 37 +DiagnosticsCount = 38 FoldingRangesCount = 2 FormatCount = 6 ImportCount = 8