[tools] go/analysis/passes/modernize: rangeint: result vars are implicit uses

2 views
Skip to first unread message

Alan Donovan (Gerrit)

unread,
Dec 18, 2025, 1:34:22 PM (23 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Alan Donovan has uploaded the change for review

Commit message

go/analysis/passes/modernize: rangeint: result vars are implicit uses

This CL fixes a bug in rangeint that caused it to make unsound
fixes because it failed to consider that result variables can be
implicit uses of a loop index variable after the loop, and thus
the fix to golang/go#71952 was insufficient.

+ test

Fixes golang/go#76880
Change-Id: I9c4d2082d93204023f50afb07318037d9134de23

Change diff

diff --git a/go/analysis/passes/modernize/rangeint.go b/go/analysis/passes/modernize/rangeint.go
index c42ec58..edffcee 100644
--- a/go/analysis/passes/modernize/rangeint.go
+++ b/go/analysis/passes/modernize/rangeint.go
@@ -18,6 +18,7 @@
"golang.org/x/tools/internal/analysis/analyzerutil"
typeindexanalyzer "golang.org/x/tools/internal/analysis/typeindex"
"golang.org/x/tools/internal/astutil"
+ "golang.org/x/tools/internal/moreiters"
"golang.org/x/tools/internal/typesinternal"
"golang.org/x/tools/internal/typesinternal/typeindex"
"golang.org/x/tools/internal/versions"
@@ -133,6 +134,14 @@
if typesinternal.IsPackageLevel(v) {
continue nextLoop
}
+
+ // If v is a named result, it is implicitly
+ // used after the loop (go.dev/issue/76880).
+ // TODO(adonovan): use go1.25 v.Kind() == types.ResultVar.
+ if moreiters.Contains(enclosingSignature(curLoop, info).Results().Variables(), v) {
+ continue nextLoop
+ }
+
used := false
for curId := range curLoop.Child(loop.Body).Preorder((*ast.Ident)(nil)) {
id := curId.Node().(*ast.Ident)
@@ -320,3 +329,24 @@
}
return false
}
+
+// enclosingSignature returns the signature of the innermost
+// function enclosing the syntax node denoted by cur
+// or nil if the node is not within a function.
+//
+// TODO(adonovan): factor with gopls/internal/util/typesutil.EnclosingSignature.
+func enclosingSignature(cur inspector.Cursor, info *types.Info) *types.Signature {
+ if c, ok := enclosingFunc(cur); ok {
+ switch n := c.Node().(type) {
+ case *ast.FuncDecl:
+ if f, ok := info.Defs[n.Name]; ok {
+ return f.Type().(*types.Signature)
+ }
+ case *ast.FuncLit:
+ if f, ok := info.Types[n]; ok {
+ return f.Type.(*types.Signature)
+ }
+ }
+ }
+ return nil
+}
diff --git a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
index 2a3de6b..11ddc69 100644
--- a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
+++ b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
@@ -262,6 +262,13 @@
}()
}

+// See go.dev/issue/76880.
+func _() (i int) {
+ for i = 0; i < 3; i++ { // nope: i is implicitly accessed after the loop
+ }
+ return
+}
+
func issue74687() {
for i := a.ID(0); i < 10; i++ { // want "for loop can be modernized using range over int"
println(i)
diff --git a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
index b8d9d7c..2b52145 100644
--- a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
@@ -261,6 +261,13 @@
}()
}

+// See go.dev/issue/76880.
+func _() (i int) {
+ for i = 0; i < 3; i++ { // nope: i is implicitly accessed after the loop
+ }
+ return
+}
+
func issue74687() {
for i := range a.ID(10) { // want "for loop can be modernized using range over int"
println(i)

Change information

Files:
  • M go/analysis/passes/modernize/rangeint.go
  • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
  • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
Change size: S
Delta: 3 files changed, 44 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
Gerrit-Change-Number: 731220
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Dec 18, 2025, 1:35:34 PM (23 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Madeline Kalil, Go LUCI, golang-co...@googlegroups.com
Attention needed from Madeline Kalil

Alan Donovan voted Auto-Submit+1

Auto-Submit+1
Open in Gerrit

Related details

Attention is currently required from:
  • Madeline Kalil
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
Gerrit-Change-Number: 731220
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
Gerrit-Attention: Madeline Kalil <mka...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 18:35:31 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 2:12:21 PM (22 hours ago) Dec 18
to Alan Donovan, goph...@pubsubhelper.golang.org, Go LUCI, Madeline Kalil, golang-co...@googlegroups.com
Attention needed from Alan Donovan and Madeline Kalil

Kirill Kolyshkin voted and added 1 comment

Votes added by Kirill Kolyshkin

Code-Review+2

1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Kirill Kolyshkin . resolved

Tested this on real code from math/rand and it appears to solve the issue.

Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
  • Madeline Kalil
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
Gerrit-Change-Number: 731220
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
Gerrit-Attention: Madeline Kalil <mka...@google.com>
Gerrit-Attention: Alan Donovan <adon...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 19:12:18 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 3:10:08 PM (21 hours ago) Dec 18
to Alan Donovan, goph...@pubsubhelper.golang.org, Go LUCI, Madeline Kalil, golang-co...@googlegroups.com
Attention needed from Alan Donovan and Madeline Kalil

Kirill Kolyshkin added 1 comment

Patchset-level comments
Kirill Kolyshkin . resolved

I've also updated CL 730960 (re-generated with changes from this CL included) for testing purposes, it passes the tests now.

Gerrit-Comment-Date: Thu, 18 Dec 2025 20:10:05 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
satisfied_requirement
unsatisfied_requirement
open
diffy

Madeline Kalil (Gerrit)

unread,
Dec 18, 2025, 3:17:27 PM (21 hours ago) Dec 18
to Alan Donovan, goph...@pubsubhelper.golang.org, Kirill Kolyshkin, Go LUCI, golang-co...@googlegroups.com
Attention needed from Alan Donovan

Madeline Kalil voted Code-Review+2

Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
    Gerrit-Change-Number: 731220
    Gerrit-PatchSet: 1
    Gerrit-Owner: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
    Gerrit-Attention: Alan Donovan <adon...@google.com>
    Gerrit-Comment-Date: Thu, 18 Dec 2025 20:17:25 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Dec 18, 2025, 3:18:49 PM (21 hours ago) Dec 18
    to goph...@pubsubhelper.golang.org, Madeline Kalil, Kirill Kolyshkin, Go LUCI, golang-co...@googlegroups.com

    Alan Donovan added 1 comment

    Patchset-level comments
    Kirill Kolyshkin . resolved

    Tested this on real code from math/rand and it appears to solve the issue.

    Alan Donovan

    Thanks!

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
    Gerrit-Change-Number: 731220
    Gerrit-PatchSet: 1
    Gerrit-Owner: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
    Gerrit-Comment-Date: Thu, 18 Dec 2025 20:18:46 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Kirill Kolyshkin <koly...@gmail.com>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Dec 18, 2025, 3:21:07 PM (21 hours ago) Dec 18
    to goph...@pubsubhelper.golang.org, Madeline Kalil, Kirill Kolyshkin, Go LUCI, golang-co...@googlegroups.com

    Alan Donovan voted and added 1 comment

    Votes added by Alan Donovan

    Commit-Queue+1
    TryBot-Bypass+1

    1 comment

    Patchset-level comments
    Alan Donovan . resolved

    Test failure is preexisting; bypassing trybot.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      Gerrit-Change-Number: 731220
      Gerrit-PatchSet: 1
      Gerrit-Owner: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
      Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
      Gerrit-Comment-Date: Thu, 18 Dec 2025 20:21:04 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      open
      diffy

      Alan Donovan (Gerrit)

      unread,
      Dec 18, 2025, 3:21:12 PM (21 hours ago) Dec 18
      to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Madeline Kalil, Kirill Kolyshkin, Go LUCI, golang-co...@googlegroups.com

      Alan Donovan submitted the change

      Change information

      Commit message:
      go/analysis/passes/modernize: rangeint: result vars are implicit uses

      This CL fixes a bug in rangeint that caused it to make unsound
      fixes because it failed to consider that result variables can be
      implicit uses of a loop index variable after the loop, and thus
      the fix to golang/go#71952 was insufficient.

      + test

      Fixes golang/go#76880
      Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      TryBot-Bypass: Alan Donovan <adon...@google.com>
      Auto-Submit: Alan Donovan <adon...@google.com>
      Reviewed-by: Madeline Kalil <mka...@google.com>
      Reviewed-by: Kirill Kolyshkin <koly...@gmail.com>
      Commit-Queue: Alan Donovan <adon...@google.com>
      Files:
      • M go/analysis/passes/modernize/rangeint.go
      • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
      • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
      Change size: S
      Delta: 3 files changed, 44 insertions(+), 0 deletions(-)
      Branch: refs/heads/master
      Submit Requirements:
      • requirement satisfiedCode-Review: +2 by Kirill Kolyshkin, +2 by Madeline Kalil
      • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result-1 by Go LUCI, TryBot-Bypass+1 by Alan Donovan
      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: I9c4d2082d93204023f50afb07318037d9134de23
      Gerrit-Change-Number: 731220
      Gerrit-PatchSet: 2
      open
      diffy
      satisfied_requirement

      Alan Donovan (Gerrit)

      unread,
      Dec 18, 2025, 3:25:55 PM (21 hours ago) Dec 18
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Alan Donovan has uploaded the change for review

      Commit message

      [internal-branch.go1.26-vendor] go/analysis/passes/modernize: rangeint: result vars are implicit uses


      This CL fixes a bug in rangeint that caused it to make unsound
      fixes because it failed to consider that result variables can be
      implicit uses of a loop index variable after the loop, and thus
      the fix to golang/go#71952 was insufficient.

      + test

      Fixes golang/go#76880

      Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      Reviewed-on: https://go-review.googlesource.com/c/tools/+/731220
      TryBot-Bypass: Alan Donovan <adon...@google.com>
      Auto-Submit: Alan Donovan <adon...@google.com>
      Reviewed-by: Madeline Kalil <mka...@google.com>
      Reviewed-by: Kirill Kolyshkin <koly...@gmail.com>
      Commit-Queue: Alan Donovan <adon...@google.com>
      (cherry picked from commit 95246c4aa04977c8425289eb81fd0054c9fcd48a)

      Change information

      Files:
      • M go/analysis/passes/modernize/rangeint.go
      • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
      • M go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
      Change size: S
      Delta: 3 files changed, 44 insertions(+), 0 deletions(-)

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newchange
      Gerrit-Project: tools
      Gerrit-Branch: internal-branch.go1.26-vendor
      Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      Gerrit-Change-Number: 731280
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Alan Donovan (Gerrit)

      unread,
      Dec 18, 2025, 3:26:31 PM (21 hours ago) Dec 18
      to goph...@pubsubhelper.golang.org, Madeline Kalil, golang-co...@googlegroups.com
      Attention needed from Madeline Kalil

      Alan Donovan voted Code-Review+2

      Code-Review+2
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Madeline Kalil
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: internal-branch.go1.26-vendor
      Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      Gerrit-Change-Number: 731280
      Gerrit-PatchSet: 1
      Gerrit-Owner: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
      Gerrit-Attention: Madeline Kalil <mka...@google.com>
      Gerrit-Comment-Date: Thu, 18 Dec 2025 20:26:28 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Madeline Kalil (Gerrit)

      unread,
      Dec 18, 2025, 4:48:55 PM (20 hours ago) Dec 18
      to Alan Donovan, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Alan Donovan

      Madeline Kalil voted Code-Review+2

      Code-Review+2
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Alan Donovan
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: internal-branch.go1.26-vendor
      Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      Gerrit-Change-Number: 731280
      Gerrit-PatchSet: 1
      Gerrit-Owner: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
      Gerrit-Attention: Alan Donovan <adon...@google.com>
      Gerrit-Comment-Date: Thu, 18 Dec 2025 21:48:52 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Alan Donovan (Gerrit)

      unread,
      Dec 18, 2025, 5:01:19 PM (19 hours ago) Dec 18
      to goph...@pubsubhelper.golang.org, Madeline Kalil, golang-co...@googlegroups.com

      Alan Donovan voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: internal-branch.go1.26-vendor
      Gerrit-Change-Id: I9c4d2082d93204023f50afb07318037d9134de23
      Gerrit-Change-Number: 731280
      Gerrit-PatchSet: 1
      Gerrit-Owner: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
      Gerrit-Comment-Date: Thu, 18 Dec 2025 22:01:15 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages