]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: add a new analyzer for check missing values after append
authorcui fliter <imcusg@gmail.com>
Fri, 26 May 2023 04:02:54 +0000 (12:02 +0800)
committerTim King <taking@google.com>
Tue, 3 Oct 2023 20:55:20 +0000 (20:55 +0000)
If there is no second parameter added during append, there will be no prompt when executing go vet. Add an analyzer to detect this situation

Update #60448

Change-Id: If9848835424f310c54e3e9377aaaad4a1516871a
Reviewed-on: https://go-review.googlesource.com/c/go/+/498416
Run-TryBot: shuang cui <imcusg@gmail.com>
Run-TryBot: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Tim King <taking@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/cmd/go/internal/test/flagdefs.go
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/appends.go [new file with mode: 0644]
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/doc.go [new file with mode: 0644]
src/cmd/vendor/modules.txt
src/cmd/vet/main.go
src/cmd/vet/testdata/appends/appends.go [new file with mode: 0644]
src/cmd/vet/vet_test.go

index 12d506862dde91a0e8434e23abf3f0f2e962092e..baa0cdf4c64d45f8e1cce4a3f8970d47b3aaf9ab 100644 (file)
@@ -40,6 +40,7 @@ var passFlagToTest = map[string]bool{
 }
 
 var passAnalyzersToVet = map[string]bool{
+       "appends":          true,
        "asmdecl":          true,
        "assign":           true,
        "atomic":           true,
diff --git a/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/appends.go b/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/appends.go
new file mode 100644 (file)
index 0000000..f0b90a4
--- /dev/null
@@ -0,0 +1,49 @@
+// Copyright 2023 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 appends defines an Analyzer that detects
+// if there is only one variable in append.
+package appends
+
+import (
+       _ "embed"
+       "go/ast"
+       "go/types"
+
+       "golang.org/x/tools/go/analysis"
+       "golang.org/x/tools/go/analysis/passes/inspect"
+       "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
+       "golang.org/x/tools/go/ast/inspector"
+)
+
+//go:embed doc.go
+var doc string
+
+var Analyzer = &analysis.Analyzer{
+       Name:     "appends",
+       Doc:      analysisutil.MustExtractDoc(doc, "appends"),
+       URL:      "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/appends",
+       Requires: []*analysis.Analyzer{inspect.Analyzer},
+       Run:      run,
+}
+
+func run(pass *analysis.Pass) (interface{}, error) {
+       inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
+
+       nodeFilter := []ast.Node{
+               (*ast.CallExpr)(nil),
+       }
+       inspect.Preorder(nodeFilter, func(n ast.Node) {
+               call := n.(*ast.CallExpr)
+               if ident, ok := call.Fun.(*ast.Ident); ok && ident.Name == "append" {
+                       if _, ok := pass.TypesInfo.Uses[ident].(*types.Builtin); ok {
+                               if len(call.Args) == 1 {
+                                       pass.ReportRangef(call, "append with no values")
+                               }
+                       }
+               }
+       })
+
+       return nil, nil
+}
diff --git a/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/doc.go b/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/doc.go
new file mode 100644 (file)
index 0000000..2e6a2e0
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2023 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 appends defines an Analyzer that detects
+// if there is only one variable in append.
+//
+// # Analyzer appends
+//
+// appends: check for missing values after append
+//
+// This checker reports calls to append that pass
+// no values to be appended to the slice.
+//
+//     s := []string{"a", "b", "c"}
+//     _ = append(s)
+//
+// Such calls are always no-ops and often indicate an
+// underlying mistake.
+package appends
index a2b1e248be8f552d5620cf1c6f5d8f8156ce116a..74f9d488f10cdc43871a958c392e9b645ff210f0 100644 (file)
@@ -52,6 +52,7 @@ golang.org/x/tools/cmd/bisect
 golang.org/x/tools/cover
 golang.org/x/tools/go/analysis
 golang.org/x/tools/go/analysis/internal/analysisflags
+golang.org/x/tools/go/analysis/passes/appends
 golang.org/x/tools/go/analysis/passes/asmdecl
 golang.org/x/tools/go/analysis/passes/assign
 golang.org/x/tools/go/analysis/passes/atomic
index 2290b95033c2a428113cd228204daf9b2641316b..c5197284b56165159ffb942e35cf0ffcf0add9ce 100644 (file)
@@ -9,6 +9,7 @@ import (
 
        "golang.org/x/tools/go/analysis/unitchecker"
 
+       "golang.org/x/tools/go/analysis/passes/appends"
        "golang.org/x/tools/go/analysis/passes/asmdecl"
        "golang.org/x/tools/go/analysis/passes/assign"
        "golang.org/x/tools/go/analysis/passes/atomic"
@@ -46,6 +47,7 @@ func main() {
        objabi.AddVersionFlag()
 
        unitchecker.Main(
+               appends.Analyzer,
                asmdecl.Analyzer,
                assign.Analyzer,
                atomic.Analyzer,
diff --git a/src/cmd/vet/testdata/appends/appends.go b/src/cmd/vet/testdata/appends/appends.go
new file mode 100644 (file)
index 0000000..09ef3d2
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2023 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.
+
+// This file contains tests for the appends checker.
+
+package appends
+
+func AppendsTest() {
+       sli := []string{"a", "b", "c"}
+       sli = append(sli) // ERROR "append with no values"
+}
index 8b29907e818c9d510f87c03be748178a6e9ae94a..4bb0de00b3b2b21245d9bf4e3f3c7f36aa2f3b6d 100644 (file)
@@ -62,6 +62,7 @@ func vetCmd(t *testing.T, arg, pkg string) *exec.Cmd {
 func TestVet(t *testing.T) {
        t.Parallel()
        for _, pkg := range []string{
+               "appends",
                "asm",
                "assign",
                "atomic",