[tools] go/analysis/passes/modernize: treat 'i += 1' as 'i++'

1 view
Skip to first unread message

Matthew “strager” Glazar (Gerrit)

unread,
Dec 23, 2025, 6:36:21 PM (2 days ago) Dec 23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Matthew “strager” Glazar has uploaded the change for review

Commit message

go/analysis/passes/modernize: treat 'i += 1' as 'i++'

The rangeint modernize pass converts the first loop but not the second, despite
them being equivalent:

for i := 0; i < 10; i++ {}
for i := 0; i < 10; i += 1 {}

Teach the pass to permit i += 1, increasing the pass's usefulness.

Fixes golang/go#76977
Change-Id: I3e993b7d73c59cef8a3e672ca413bb16be897b1e

Change diff

diff --git a/go/analysis/passes/modernize/rangeint.go b/go/analysis/passes/modernize/rangeint.go
index edffcee..fccd6e5 100644
--- a/go/analysis/passes/modernize/rangeint.go
+++ b/go/analysis/passes/modernize/rangeint.go
@@ -123,9 +123,21 @@
continue nextLoop
}

+ validIncrement := false
if inc, ok := loop.Post.(*ast.IncDecStmt); ok &&
inc.Tok == token.INC &&
astutil.EqualSyntax(compare.X, inc.X) {
+ // Have: i++
+ validIncrement = true
+ } else if assign, ok := loop.Post.(*ast.AssignStmt); ok &&
+ assign.Tok == token.ADD_ASSIGN &&
+ len(assign.Rhs) == 1 && isIntLiteral(info, assign.Rhs[0], 1) &&
+ len(assign.Lhs) == 1 && astutil.EqualSyntax(compare.X, assign.Lhs[0]) {
+ // Have: i += 1
+ validIncrement = true
+ }
+
+ if validIncrement {
// Have: for i = 0; i < limit; i++ {}

// Find references to i within the loop body.
@@ -246,7 +258,7 @@

pass.Report(analysis.Diagnostic{
Pos: init.Pos(),
- End: inc.End(),
+ End: loop.Post.End(),
Message: "for loop can be modernized using range over int",
SuggestedFixes: []analysis.SuggestedFix{{
Message: fmt.Sprintf("Replace for loop with range %s",
@@ -272,7 +284,7 @@
// Delete inc.
{
Pos: limit.End(),
- End: inc.End(),
+ End: loop.Post.End(),
},
// Add ")" after limit, if needed.
{
diff --git a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
index 11ddc69..a14dc38 100644
--- a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
+++ b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go
@@ -92,6 +92,19 @@
i = 8
}

+ for i := 0; i < 10; i += 1 { // want "for loop can be modernized using range over int"
+ println(i)
+ }
+ for i := 0; i < 10; s.i += 1 { // nope: modifies another variable
+ println(i)
+ }
+ for i := 0; i < 10; i -= 1 { // nope: decrements i
+ println(i)
+ }
+ for i := 0; i < 10; i += 2 { // nope: range only increments by 1
+ println(i)
+ }
+
// The limit expression must be loop invariant;
// see https://github.com/golang/go/issues/72917
for i := 0; i < f(); i++ { // nope
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 2b52145..0e92147 100644
--- a/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/rangeint/rangeint.go.golden
@@ -90,7 +90,20 @@
for i := 0; i < 10; i++ { // nope: assigns i
i = 8
}
-
+
+ for i := range 10 { // want "for loop can be modernized using range over int"
+ println(i)
+ }
+ for i := 0; i < 10; s.i += 1 { // nope: modifies another variable
+ println(i)
+ }
+ for i := 0; i < 10; i -= 1 { // nope: decrements i
+ println(i)
+ }
+ for i := 0; i < 10; i += 2 { // nope: range only increments by 1
+ println(i)
+ }
+
// The limit expression must be loop invariant;
// see https://github.com/golang/go/issues/72917
for i := 0; i < f(); i++ { // nope

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, 41 insertions(+), 3 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: I3e993b7d73c59cef8a3e672ca413bb16be897b1e
Gerrit-Change-Number: 732500
Gerrit-PatchSet: 1
Gerrit-Owner: Matthew “strager” Glazar <str...@traduality.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Dec 23, 2025, 6:38:16 PM (2 days ago) Dec 23
to Matthew “strager” Glazar, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Message from Gopher Robot

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.

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: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I3e993b7d73c59cef8a3e672ca413bb16be897b1e
Gerrit-Change-Number: 732500
Gerrit-PatchSet: 1
Gerrit-Owner: Matthew “strager” Glazar <str...@traduality.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Comment-Date: Tue, 23 Dec 2025 23:38:11 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Dec 23, 2025, 9:42:09 PM (2 days ago) Dec 23
to Matthew “strager” Glazar, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Matthew “strager” Glazar

Alan Donovan voted and added 1 comment

Votes added by Alan Donovan

Code-Review+2

1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Alan Donovan . resolved

Very nice; many thanks for contributing this fix.

Open in Gerrit

Related details

Attention is currently required from:
  • Matthew “strager” Glazar
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: I3e993b7d73c59cef8a3e672ca413bb16be897b1e
Gerrit-Change-Number: 732500
Gerrit-PatchSet: 1
Gerrit-Owner: Matthew “strager” Glazar <str...@traduality.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Matthew “strager” Glazar <str...@traduality.com>
Gerrit-Comment-Date: Wed, 24 Dec 2025 02:42:04 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Hongxiang Jiang (Gerrit)

unread,
Dec 24, 2025, 12:59:35 AM (yesterday) Dec 24
to Matthew “strager” Glazar, goph...@pubsubhelper.golang.org, Alan Donovan, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Matthew “strager” Glazar

Hongxiang Jiang voted

Code-Review+1
Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Matthew “strager” Glazar
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: I3e993b7d73c59cef8a3e672ca413bb16be897b1e
    Gerrit-Change-Number: 732500
    Gerrit-PatchSet: 1
    Gerrit-Owner: Matthew “strager” Glazar <str...@traduality.com>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Hongxiang Jiang <hxj...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Matthew “strager” Glazar <str...@traduality.com>
    Gerrit-Comment-Date: Wed, 24 Dec 2025 05:59:29 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages