[tools] go/analysis/passes/modernize: fix slicesbackward unused value

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Apr 29, 2026, 5:25:22 PM (19 hours ago) Apr 29
to Madeline Kalil, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, golang...@luci-project-accounts.iam.gserviceaccount.com, Alan Donovan, golang-co...@googlegroups.com

Gopher Robot submitted the change with unreviewed changes

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
Reviewed-by: Alan Donovan <adon...@google.com>
Auto-Submit: Madeline Kalil <mka...@google.com>
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
Submit Requirements:
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I53aec72f2645fffda655e6108ded87bbf42e4c7e
Gerrit-Change-Number: 771760
Gerrit-PatchSet: 7
Gerrit-Owner: Madeline Kalil <mka...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages