Skip to content

Commit 1501321

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/analysis/modernize: fix bug in minmax
Fix another bug related to not checking AssignStmt.Op. + test Updates golang/go#70815 Change-Id: I426d27b4879d30f9fecb4d0f131b3c1a0b07773b Reviewed-on: https://go-review.googlesource.com/c/tools/+/642078 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent b31dda4 commit 1501321

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

gopls/internal/analysis/modernize/maps.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,19 @@ func mapsloop(pass *analysis.Pass) {
177177
for curRange := range curFile.Preorder((*ast.RangeStmt)(nil)) {
178178
rng := curRange.Node().(*ast.RangeStmt)
179179

180-
if rng.Tok == token.DEFINE && rng.Key != nil && rng.Value != nil && len(rng.Body.List) == 1 {
181-
// Have: for k, v := range x { S }
182-
if assign, ok := rng.Body.List[0].(*ast.AssignStmt); ok &&
183-
assign.Tok == token.ASSIGN &&
184-
len(assign.Lhs) == 1 {
185-
// Have: for k, v := range x { lhs = rhs }
186-
187-
if index, ok := assign.Lhs[0].(*ast.IndexExpr); ok &&
188-
equalSyntax(rng.Key, index.Index) &&
189-
equalSyntax(rng.Value, assign.Rhs[0]) {
190-
191-
// Have: for k, v := range x { m[k] = v }
192-
check(file, curRange, assign, index.X, rng.X)
193-
}
180+
if rng.Tok == token.DEFINE &&
181+
rng.Key != nil &&
182+
rng.Value != nil &&
183+
isAssignBlock(rng.Body) {
184+
// Have: for k, v := range x { lhs = rhs }
185+
186+
assign := rng.Body.List[0].(*ast.AssignStmt)
187+
if index, ok := assign.Lhs[0].(*ast.IndexExpr); ok &&
188+
equalSyntax(rng.Key, index.Index) &&
189+
equalSyntax(rng.Value, assign.Rhs[0]) {
190+
191+
// Have: for k, v := range x { m[k] = v }
192+
check(file, curRange, assign, index.X, rng.X)
194193
}
195194
}
196195
}

gopls/internal/analysis/modernize/minmax.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func minmax(pass *analysis.Pass) {
9595
})
9696
}
9797

98-
} else if prev, ok := curIfStmt.PrevSibling(); ok && is[*ast.AssignStmt](prev.Node()) {
98+
} else if prev, ok := curIfStmt.PrevSibling(); ok && isSimpleAssign(prev.Node()) {
9999
fassign := prev.Node().(*ast.AssignStmt)
100100

101101
// Have: lhs0 = rhs0; if a < b { lhs = rhs }
@@ -193,8 +193,17 @@ func isAssignBlock(b *ast.BlockStmt) bool {
193193
if len(b.List) != 1 {
194194
return false
195195
}
196-
assign, ok := b.List[0].(*ast.AssignStmt)
197-
return ok && assign.Tok == token.ASSIGN && len(assign.Lhs) == 1 && len(assign.Rhs) == 1
196+
// Inv: the sole statement cannot be { lhs := rhs }.
197+
return isSimpleAssign(b.List[0])
198+
}
199+
200+
// isSimpleAssign reports whether n has the form "lhs = rhs" or "lhs := rhs".
201+
func isSimpleAssign(n ast.Node) bool {
202+
assign, ok := n.(*ast.AssignStmt)
203+
return ok &&
204+
(assign.Tok == token.ASSIGN || assign.Tok == token.DEFINE) &&
205+
len(assign.Lhs) == 1 &&
206+
len(assign.Rhs) == 1
198207
}
199208

200209
// -- utils --

gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,14 @@ func oops() {
8181
}
8282
print(y)
8383
}
84+
85+
// Regression test for a bug: += is not a simple assignment.
86+
func nopeAssignHasIncrementOperator() {
87+
x := 1
88+
y := 0
89+
y += 2
90+
if x > y {
91+
y = x
92+
}
93+
print(y)
94+
}

gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,14 @@ func oops() {
5858
y := max(x, 2)
5959
print(y)
6060
}
61+
62+
// Regression test for a bug: += is not a simple assignment.
63+
func nopeAssignHasIncrementOperator() {
64+
x := 1
65+
y := 0
66+
y += 2
67+
if x > y {
68+
y = x
69+
}
70+
print(y)
71+
}

0 commit comments

Comments
 (0)