Skip to content

Commit 7231669

Browse files
cuishuanggopherbot
authored andcommitted
gopls/internal/analysis/modernize: don't offer a fix when initialization statement is not empty
Fixes golang/go#73547 Change-Id: I878f7ab71c1dce896f5eef7fb319cf99b2394f88 Reviewed-on: https://go-review.googlesource.com/c/tools/+/669055 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent deec52f commit 7231669

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

gopls/internal/analysis/modernize/stringscutprefix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func stringscutprefix(pass *analysis.Pass) {
5959
ifStmt := curIfStmt.Node().(*ast.IfStmt)
6060

6161
// pattern1
62-
if call, ok := ifStmt.Cond.(*ast.CallExpr); ok && len(ifStmt.Body.List) > 0 {
62+
if call, ok := ifStmt.Cond.(*ast.CallExpr); ok && ifStmt.Init == nil && len(ifStmt.Body.List) > 0 {
63+
6364
obj := typeutil.Callee(info, call)
6465
if !analysisinternal.IsFunctionNamed(obj, "strings", "HasPrefix") &&
6566
!analysisinternal.IsFunctionNamed(obj, "bytes", "HasPrefix") {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func _() {
5959
a := strings.TrimPrefix(s, pre) // noop, as the argument isn't the same
6060
_ = a
6161
}
62+
if s1 := s; strings.HasPrefix(s1, pre) {
63+
a := strings.TrimPrefix(s1, pre) // noop, as IfStmt.Init is present
64+
_ = a
65+
}
6266
}
6367

6468
var value0 string

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func _() {
5959
a := strings.TrimPrefix(s, pre) // noop, as the argument isn't the same
6060
_ = a
6161
}
62+
if s1 := s; strings.HasPrefix(s1, pre) {
63+
a := strings.TrimPrefix(s1, pre) // noop, as IfStmt.Init is present
64+
_ = a
65+
}
6266
}
6367

6468
var value0 string

0 commit comments

Comments
 (0)