go/analysis/passes/modernize: add slicesbackward analyzer
Adds a new slicesbackward pass to the modernize suite that suggests
replacing manually-written backward loops over slices with
slices.Backward (Go 1.23).
Before:
for i := len(s) - 1; i >= 0; i-- {
use(s[i])
}
After:
for _, v := range slices.Backward(s) {
use(v)
}
The analyzer matches for loops with the exact pattern:
- init: i := len(s) - 1 (where s is a slice expression)
- cond: i >= 0
- post: i--
When every use of i in the body is s[i], those are replaced with a
fresh value variable and the range clause becomes "_, v :=".
When i is used for other purposes too, both index and value are kept:
"i, v := range slices.Backward(s)".
Loops where i is assigned or address-taken inside the body are skipped
to avoid changing program behavior. Loops using = (not :=) in the init
where i is address-taken before the loop are also skipped.
The analyzer is unexported; it is accessible via goplsexport until the
proposal is approved and the API is published.
Fixes golang/go#78484
Change-Id: Ic920cc149c4a3ff2524f4d17f347ec48ab43d05a
GitHub-Last-Rev: 4b3470ab6b3edb7c7492742b1255bb2f0777b2be
GitHub-Pull-Request: golang/tools#627
Reviewed-on: https://go-review.googlesource.com/c/tools/+/761740
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This repository provides the golang.org/x/tools module, comprising various tools and packages mostly for static analysis of Go programs, some of which are listed below. Use the “Go reference” link above for more information about any package.
It also contains the golang.org/x/tools/gopls module, whose root package is a language-server protocol (LSP) server for Go. An LSP server analyses the source code of a project and responds to requests from a wide range of editors such as VSCode and Vim, allowing them to support IDE-like functionality.
Selected commands:
cmd/goimports formats a Go program like go fmt and additionally inserts import statements for any packages required by the file after it is edited.cmd/callgraph prints the call graph of a Go program.cmd/digraph is a utility for manipulating directed graphs in textual notation.cmd/stringer generates declarations (including a String method) for “enum” types.cmd/toolstash is a utility to simplify working with multiple versions of the Go toolchain.These commands may be fetched with a command such as
go install golang.org/x/tools/cmd/goimports@latest
Selected packages:
go/ssa provides a static single-assignment form (SSA) intermediate representation (IR) for Go programs, similar to a typical compiler, for use by analysis tools.
go/packages provides a simple interface for loading, parsing, and type checking a complete Go program from source code.
go/analysis provides a framework for modular static analysis of Go programs.
go/callgraph provides call graphs of Go programs using a variety of algorithms with different trade-offs.
go/ast/inspector provides an optimized means of traversing a Go parse tree for use in analysis tools.
go/cfg provides a simple control-flow graph (CFG) for a Go function.
go/gcexportdata and go/gccgoexportdata read and write the binary files containing type information used by the standard and gccgo compilers.
go/types/objectpath provides a stable naming scheme for named entities (“objects”) in the go/types API.
Numerous other packages provide more esoteric functionality.
This repository uses Gerrit for code changes. To learn how to submit changes, see https://go.dev/doc/contribute.
The git repository is https://go.googlesource.com/tools.
The main issue tracker for the tools repository is located at https://go.dev/issues. Prefix your issue with “x/tools/(your subdir):” in the subject line, so it is easy to find.
This repository uses prettier to format JS and CSS files.
The version of prettier used is 1.18.2.
It is encouraged that all JS and CSS code be run through this before submitting a change. However, it is not a strict requirement enforced by CI.