diff options
| author | cui fliter <imcusg@gmail.com> | 2023-05-26 12:02:54 +0800 |
|---|---|---|
| committer | Tim King <taking@google.com> | 2023-10-03 20:55:20 +0000 |
| commit | dc523c8ddf5b0ce985ea90c65cbaa097c9e3ee09 (patch) | |
| tree | 46f66080b162b271358769791f9405d465ca948a /src/cmd/vendor | |
| parent | e9379a8f8bd5f00463f67fe7e052cbcb262b169a (diff) | |
| download | go-dc523c8ddf5b0ce985ea90c65cbaa097c9e3ee09.tar.xz | |
cmd: add a new analyzer for check missing values after append
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>
Diffstat (limited to 'src/cmd/vendor')
3 files changed, 70 insertions, 0 deletions
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 index 0000000000..f0b90a4920 --- /dev/null +++ b/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/appends.go @@ -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 index 0000000000..2e6a2e010b --- /dev/null +++ b/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/appends/doc.go @@ -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 diff --git a/src/cmd/vendor/modules.txt b/src/cmd/vendor/modules.txt index a2b1e248be..74f9d488f1 100644 --- a/src/cmd/vendor/modules.txt +++ b/src/cmd/vendor/modules.txt @@ -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 |
