Unreviewed changes
5 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:
```
The name of the file: go/analysis/passes/modernize/slicesbackward.go
Insertions: 6, Deletions: 4.
@@ -203,15 +203,17 @@
})
}
- // Replace the loop header with a range over slices.Backward.
+ // Replace the loop header with a range over slices.Backward. In
+ // well-typed code, at least one of the index or value variables must be
+ // referenced inside the loop body (otherUses + sliceIndexes > 0).
var vars string
- if otherUses == 0 && len(sliceIndexes) > 0 {
+ if otherUses == 0 { // sliceIndexes > 0
// All uses of i are s[i]; drop the index variable.
vars = fmt.Sprintf("_, %s", elemName)
- } else if otherUses > 0 && len(sliceIndexes) == 0 {
+ } else if len(sliceIndexes) == 0 { // otherUses > 0
// Index i is not used in any s[i] expressions; drop the value variable.
vars = indexIdent.Name
- } else {
+ } else { // otherUses > 0 && sliceIndexes > 0, keep both variables.
vars = fmt.Sprintf("%s, %s", indexIdent.Name, elemName)
}
header := fmt.Sprintf("%s := range %sBackward(%s)", vars, prefix, sliceStr)
```
Change information
Commit message:
go/analysis/passes/modernize: fix slicesbackward unused value
The slicesbackward modernizer should omit the declaration of v
if only the slice index i is used within the loop.
Adapted from Jah-yee's work in CL 770700 and CL 768700
For golang/go#78629
Change-Id: I53aec72f2645fffda655e6108ded87bbf42e4c7e
Files:
- M go/analysis/passes/modernize/slicesbackward.go
- M go/analysis/passes/modernize/testdata/src/slicesbackward/slicesbackward.go.golden
Change size: S
Delta: 2 files changed, 29 insertions(+), 13 deletions(-)
Branch: refs/heads/master