Skip to content

go/analysis/passes/modernize: add slicesbackward analyzer#627

Closed
abhay1999 wants to merge 1 commit into
golang:masterfrom
abhay1999:add-slicesbackward-modernizer
Closed

go/analysis/passes/modernize: add slicesbackward analyzer#627
abhay1999 wants to merge 1 commit into
golang:masterfrom
abhay1999:add-slicesbackward-modernizer

Conversation

@abhay1999

@abhay1999 abhay1999 commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

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

@google-cla

google-cla Bot commented Apr 1, 2026

Copy link
Copy Markdown

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.

@gopherbot

Copy link
Copy Markdown
Contributor

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:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alan Donovan:

Patch Set 1:

(16 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@abhay1999 abhay1999 force-pushed the add-slicesbackward-modernizer branch from a291394 to 324ca70 Compare April 1, 2026 15:12
@gopherbot

Copy link
Copy Markdown
Contributor

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:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Abhay Chaurasiya:

Patch Set 2:

(18 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@abhay1999 abhay1999 force-pushed the add-slicesbackward-modernizer branch from 324ca70 to f0618d3 Compare April 1, 2026 16:35
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>
@abhay1999 abhay1999 force-pushed the add-slicesbackward-modernizer branch from f0618d3 to 4b3470a Compare April 1, 2026 16:57
@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alan Donovan:

Patch Set 2:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Abhay Chaurasiya:

Patch Set 3:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alan Donovan:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Abhay Chaurasiya:

Patch Set 4:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Go LUCI:

Patch Set 4: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Abhay Chaurasiya:

Patch Set 4:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/761740.
After addressing review feedback, remember to publish your drafts!

gopherbot pushed a commit that referenced this pull request Apr 3, 2026
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>
@gopherbot

Copy link
Copy Markdown
Contributor

This PR is being closed because golang.org/cl/761740 has been merged.

@gopherbot gopherbot closed this Apr 3, 2026
@abhay1999 abhay1999 deleted the add-slicesbackward-modernizer branch April 4, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

proposal: x/tools/go/analysis/passes/modernize: add slicesbackward modernizer

2 participants