Skip to content

Commit 43ba465

Browse files
adonovangopherbot
authored andcommitted
internal/analysis/modernize: minmax: reject IfStmt with Init
Updates golang/go#71111 Change-Id: I86cdf1731e6057c4c972b91c46e8d3216a18382d Reviewed-on: https://go-review.googlesource.com/c/tools/+/640037 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Commit-Queue: Alan Donovan <[email protected]>
1 parent 29d66ee commit 43ba465

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Diff for: gopls/internal/analysis/modernize/minmax.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ func minmax(pass *analysis.Pass) {
8484
// Replace IfStmt with lhs = min(a, b).
8585
Pos: ifStmt.Pos(),
8686
End: ifStmt.End(),
87-
NewText: []byte(fmt.Sprintf("%s = %s(%s, %s)",
87+
NewText: fmt.Appendf(nil, "%s = %s(%s, %s)",
8888
formatNode(pass.Fset, lhs),
8989
sym,
9090
formatNode(pass.Fset, a),
91-
formatNode(pass.Fset, b))),
91+
formatNode(pass.Fset, b)),
9292
}},
9393
}},
9494
})
@@ -133,10 +133,10 @@ func minmax(pass *analysis.Pass) {
133133
// Replace rhs2 and IfStmt with min(a, b)
134134
Pos: rhs2.Pos(),
135135
End: ifStmt.End(),
136-
NewText: []byte(fmt.Sprintf("%s(%s, %s)",
136+
NewText: fmt.Appendf(nil, "%s(%s, %s)",
137137
sym,
138138
formatNode(pass.Fset, a),
139-
formatNode(pass.Fset, b))),
139+
formatNode(pass.Fset, b)),
140140
}},
141141
}},
142142
})
@@ -151,6 +151,7 @@ func minmax(pass *analysis.Pass) {
151151
ifStmt := curIfStmt.Node().(*ast.IfStmt)
152152

153153
if compare, ok := ifStmt.Cond.(*ast.BinaryExpr); ok &&
154+
ifStmt.Init == nil &&
154155
isInequality(compare.Op) != 0 &&
155156
isAssignBlock(ifStmt.Body) {
156157

Diff for: gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go

+8
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ func shadowed() int {
6363
}
6464
return time
6565
}
66+
67+
func nopeIfStmtHasInitStmt() {
68+
x := 1
69+
if y := 2; y < x { // silent: IfStmt has an Init stmt
70+
x = y
71+
}
72+
print(x)
73+
}

Diff for: gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden

+8
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ func shadowed() int {
4343
}
4444
return time
4545
}
46+
47+
func nopeIfStmtHasInitStmt() {
48+
x := 1
49+
if y := 2; y < x { // silent: IfStmt has an Init stmt
50+
x = y
51+
}
52+
print(x)
53+
}

0 commit comments

Comments
 (0)