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 <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Commit-Queue: Alan Donovan <adonovan@google.com>
diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go
index 6760900..0633065 100644
--- a/gopls/internal/analysis/modernize/minmax.go
+++ b/gopls/internal/analysis/modernize/minmax.go
@@ -84,11 +84,11 @@
// Replace IfStmt with lhs = min(a, b).
Pos: ifStmt.Pos(),
End: ifStmt.End(),
- NewText: []byte(fmt.Sprintf("%s = %s(%s, %s)",
+ NewText: fmt.Appendf(nil, "%s = %s(%s, %s)",
formatNode(pass.Fset, lhs),
sym,
formatNode(pass.Fset, a),
- formatNode(pass.Fset, b))),
+ formatNode(pass.Fset, b)),
}},
}},
})
@@ -133,10 +133,10 @@
// Replace rhs2 and IfStmt with min(a, b)
Pos: rhs2.Pos(),
End: ifStmt.End(),
- NewText: []byte(fmt.Sprintf("%s(%s, %s)",
+ NewText: fmt.Appendf(nil, "%s(%s, %s)",
sym,
formatNode(pass.Fset, a),
- formatNode(pass.Fset, b))),
+ formatNode(pass.Fset, b)),
}},
}},
})
@@ -151,6 +151,7 @@
ifStmt := curIfStmt.Node().(*ast.IfStmt)
if compare, ok := ifStmt.Cond.(*ast.BinaryExpr); ok &&
+ ifStmt.Init == nil &&
isInequality(compare.Op) != 0 &&
isAssignBlock(ifStmt.Body) {
diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go
index d2efeb7..393b372 100644
--- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go
+++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go
@@ -63,3 +63,11 @@
}
return time
}
+
+func nopeIfStmtHasInitStmt() {
+ x := 1
+ if y := 2; y < x { // silent: IfStmt has an Init stmt
+ x = y
+ }
+ print(x)
+}
diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden
index c29d42d..aacf84d 100644
--- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden
+++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden
@@ -43,3 +43,11 @@
}
return time
}
+
+func nopeIfStmtHasInitStmt() {
+ x := 1
+ if y := 2; y < x { // silent: IfStmt has an Init stmt
+ x = y
+ }
+ print(x)
+}