aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/analysis/noresultvalues
diff options
context:
space:
mode:
authorRohan Challa <rohan@golang.org>2020-03-16 14:46:49 -0400
committerRohan Challa <rohan@golang.org>2020-03-27 18:57:59 +0000
commitf4fcf867e710f011999090fe3bef367096314b48 (patch)
tree011e20d381f10ae2016ad4f71e6f164bdc4360e2 /internal/lsp/analysis/noresultvalues
parentafab6edfada8bd76fd15bf977ec4f074c9ad3908 (diff)
downloadgolang-x-tools-f4fcf867e710f011999090fe3bef367096314b48.tar.gz
internal/lsp/analysis: add quickfix for "no result values expected"
This change adds a quick fix for type errors of the type "no result values expected". It will replace the return statment with an empty return statement. Updates golang/go#34644 Change-Id: I3885748dfc69a2d19f8e7a2e81f36f6d0a20d25b Reviewed-on: https://go-review.googlesource.com/c/tools/+/223666 Run-TryBot: Rohan Challa <rohan@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Diffstat (limited to 'internal/lsp/analysis/noresultvalues')
-rw-r--r--internal/lsp/analysis/noresultvalues/noresultvalues.go85
-rw-r--r--internal/lsp/analysis/noresultvalues/noresultvalues_test.go17
-rw-r--r--internal/lsp/analysis/noresultvalues/testdata/src/a/a.go9
-rw-r--r--internal/lsp/analysis/noresultvalues/testdata/src/a/a.go.golden9
4 files changed, 120 insertions, 0 deletions
diff --git a/internal/lsp/analysis/noresultvalues/noresultvalues.go b/internal/lsp/analysis/noresultvalues/noresultvalues.go
new file mode 100644
index 000000000..0259691ed
--- /dev/null
+++ b/internal/lsp/analysis/noresultvalues/noresultvalues.go
@@ -0,0 +1,85 @@
+// Copyright 2020 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 noresultvalues defines an Analyzer that applies suggested fixes
+// to errors of the type "no result values expected".
+package noresultvalues
+
+import (
+ "bytes"
+ "go/ast"
+ "go/format"
+
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/ast/inspector"
+ "golang.org/x/tools/internal/analysisinternal"
+)
+
+const Doc = `suggested fixes for "no result values expected"
+
+This checker provides suggested fixes for type errors of the
+type "no result values expected". For example:
+ func z() { return nil }
+will turn into
+ func z() { return }
+`
+
+var Analyzer = &analysis.Analyzer{
+ Name: string(analysisinternal.NoResultValues),
+ Doc: Doc,
+ Requires: []*analysis.Analyzer{inspect.Analyzer},
+ Run: run,
+ RunDespiteErrors: true,
+}
+
+const noResultValuesMsg = "no result values expected"
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
+ errors := analysisinternal.GetTypeErrors(pass)
+
+ nodeFilter := []ast.Node{(*ast.ReturnStmt)(nil)}
+ inspect.Preorder(nodeFilter, func(n ast.Node) {
+ retStmt, _ := n.(*ast.ReturnStmt)
+
+ var file *ast.File
+ for _, f := range pass.Files {
+ if f.Pos() <= retStmt.Pos() && retStmt.Pos() < f.End() {
+ file = f
+ break
+ }
+ }
+ if file == nil {
+ return
+ }
+
+ for _, err := range errors {
+ if err.Msg != noResultValuesMsg {
+ continue
+ }
+ if retStmt.Pos() >= err.Pos || err.Pos >= retStmt.End() {
+ continue
+ }
+ var buf bytes.Buffer
+ if err := format.Node(&buf, pass.Fset, file); err != nil {
+ continue
+ }
+ pass.Report(analysis.Diagnostic{
+ Pos: err.Pos,
+ End: analysisinternal.TypeErrorEndPos(pass.Fset, buf.Bytes(), err.Pos),
+ Message: err.Msg,
+ SuggestedFixes: []analysis.SuggestedFix{{
+ Message: "Delete return values",
+ TextEdits: []analysis.TextEdit{{
+ Pos: retStmt.Pos(),
+ End: retStmt.End(),
+ NewText: []byte("return"),
+ }},
+ }},
+ })
+ }
+ })
+ return nil, nil
+}
diff --git a/internal/lsp/analysis/noresultvalues/noresultvalues_test.go b/internal/lsp/analysis/noresultvalues/noresultvalues_test.go
new file mode 100644
index 000000000..6b9451bf2
--- /dev/null
+++ b/internal/lsp/analysis/noresultvalues/noresultvalues_test.go
@@ -0,0 +1,17 @@
+// Copyright 2020 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 noresultvalues_test
+
+import (
+ "testing"
+
+ "golang.org/x/tools/go/analysis/analysistest"
+ "golang.org/x/tools/internal/lsp/analysis/noresultvalues"
+)
+
+func Test(t *testing.T) {
+ testdata := analysistest.TestData()
+ analysistest.RunWithSuggestedFixes(t, testdata, noresultvalues.Analyzer, "a")
+}
diff --git a/internal/lsp/analysis/noresultvalues/testdata/src/a/a.go b/internal/lsp/analysis/noresultvalues/testdata/src/a/a.go
new file mode 100644
index 000000000..30265a42f
--- /dev/null
+++ b/internal/lsp/analysis/noresultvalues/testdata/src/a/a.go
@@ -0,0 +1,9 @@
+// Copyright 2020 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 noresultvalues
+
+func x() { return nil } // want "no result values expected"
+
+func y() { return nil, "hello" } // want "no result values expected"
diff --git a/internal/lsp/analysis/noresultvalues/testdata/src/a/a.go.golden b/internal/lsp/analysis/noresultvalues/testdata/src/a/a.go.golden
new file mode 100644
index 000000000..6b29cefa3
--- /dev/null
+++ b/internal/lsp/analysis/noresultvalues/testdata/src/a/a.go.golden
@@ -0,0 +1,9 @@
+// Copyright 2020 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 noresultvalues
+
+func x() { return } // want "no result values expected"
+
+func y() { return } // want "no result values expected"