[tools/internal-branch.go1.27-vendor] [internal-branch.go1.27-vendor] go/analysis/passes/modernize: slicesbackward - skip if s[i] is mutated

0 views
Skip to first unread message

Carlos Amedee (Gerrit)

unread,
Jul 24, 2026, 4:48:25 PM (2 days ago) Jul 24
to Madeline Kalil, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, golang...@luci-project-accounts.iam.gserviceaccount.com, Alan Donovan, golang-co...@googlegroups.com

Carlos Amedee submitted the change

Change information

Commit message:
[internal-branch.go1.27-vendor] go/analysis/passes/modernize: slicesbackward - skip if s[i] is mutated

In the slicesbackward modernizer, we replace backward loops over
slices with a call to slices.Backward. However, if the index
expression s[i] is address-taken or mutated within the loop,
we should not offer this fix. For example:

for i := len(s) - 1; i > 0; i-- {
s[i].n = 5
}

for _, v := range slices.Backward(s) {
v.n = 5
}

v is a local copy, so the original s will not get mutated.
To avoid changing the program behavior, we should
not suggest a modernization.

Promote a new function IsAssignedOrAddressTaken that replaces the uses
of isScalarLValue in modernizers and isLValueUse
in the inliner and add tests for it.

Fixes golang/go#80410

Change-Id: I873afe8965dee39994547e16f284ca80541334d6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/802260
LUCI-TryBot-Result: golang...@luci-project-accounts.iam.gserviceaccount.com <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adon...@google.com>
(cherry picked from commit b96d2a55a08943af1de2914a59fb88fe0acbb897)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/805520
Reviewed-by: Carlos Amedee <car...@golang.org>
Files:
  • M go/analysis/passes/modernize/minmax.go
  • M go/analysis/passes/modernize/modernize.go
  • M go/analysis/passes/modernize/rangeint.go
  • M go/analysis/passes/modernize/slicesbackward.go
  • M go/analysis/passes/modernize/stringscut.go
  • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
  • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
  • M go/analysis/passes/modernize/testdata/src/slicesbackward/slicesbackward.go
  • M go/analysis/passes/modernize/testdata/src/slicesbackward/slicesbackward.go.golden
  • M gopls/internal/golang/inline.go
  • A internal/typesinternal/assignedaddress.go
  • A internal/typesinternal/assignedaddress_test.go
Change size: L
Delta: 12 files changed, 603 insertions(+), 107 deletions(-)
Branch: refs/heads/internal-branch.go1.27-vendor
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: internal-branch.go1.27-vendor
Gerrit-Change-Id: I873afe8965dee39994547e16f284ca80541334d6
Gerrit-Change-Number: 805520
Gerrit-PatchSet: 2
Gerrit-Owner: Madeline Kalil <mka...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages