go/analysis/passes/modernize: add slicesbackward analyzer#627
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
This PR (HEAD: a291394) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/761740. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Alan Donovan: Patch Set 1: (16 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
a291394 to
324ca70
Compare
|
This PR (HEAD: 324ca70) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/761740. Important tips:
|
|
Message from Abhay Chaurasiya: Patch Set 2: (18 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
324ca70 to
f0618d3
Compare
Add a new modernize pass, slicesbackward, that suggests replacing
manually-written backward loops over slices:
for i := len(s) - 1; i >= 0; i-- {
use(s[i])
}
with the more readable Go 1.23 slices.Backward iterator:
for _, v := range slices.Backward(s) {
use(v)
}
The analyzer matches loops with the exact pattern:
- init: i := len(s) - 1 (where s is a slice)
- cond: i >= 0
- post: i--
If every use of i in the body is of the form s[i], those are replaced
with a value variable and the range clause becomes "_, v :=". If i is
also used for other purposes, both index and value are kept: "i, v :=".
Loops where i is assigned or address-taken inside the body are skipped.
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
Signed-off-by: abhay1999 <abhaychaurasiya19@gmail.com>
f0618d3 to
4b3470a
Compare
|
Message from Alan Donovan: Patch Set 2: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Abhay Chaurasiya: Patch Set 3: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Alan Donovan: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
This PR (HEAD: 4b3470a) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/761740. Important tips:
|
|
Message from Abhay Chaurasiya: Patch Set 4: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Alan Donovan: Patch Set 4: Auto-Submit+1 Code-Review+2 Commit-Queue+1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Go LUCI: Patch Set 4: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2026-04-01T17:16:24Z","revision":"f10c41912ec22f7b7bcec6c6c9ad572170bf3fa7"} Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Alan Donovan: Patch Set 4: -Commit-Queue (Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_57259>) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Go LUCI: Patch Set 4: This CL has passed the run Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Go LUCI: Patch Set 4: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
|
Message from Abhay Chaurasiya: Patch Set 4: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/761740. |
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: 4b3470a
GitHub-Pull-Request: #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 PR is being closed because golang.org/cl/761740 has been merged. |
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:
After:
The analyzer matches for loops with the exact pattern:
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