Skip to content

Commit a99a1c3

Browse files
karamaru-alphagopherbot
authored andcommitted
gopls/internal/analysis/modernize: prevent conversion of variadic functions to slices.ContainsFunc
Exclude variadic functions from being converted to slices.ContainsFunc to avoid type errors. Fixes golang/go#73269 Change-Id: I2259d6639515939717305ab75553393485aca8e8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/663436 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 4e973d9 commit a99a1c3

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

gopls/internal/analysis/modernize/slicescontains.go

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ func slicescontains(pass *analysis.Pass) {
129129
isSliceElem(cond.Args[0]) &&
130130
typeutil.Callee(info, cond) != nil { // not a conversion
131131

132+
// skip variadic functions
133+
if sig, ok := info.TypeOf(cond.Fun).(*types.Signature); ok && sig.Variadic() {
134+
return
135+
}
136+
132137
funcName = "ContainsFunc"
133138
arg2 = cond.Fun // "if predicate(elem)"
134139
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,26 @@ func nopeNeedleHaystackDifferentTypes2(x error, args []any) {
146146
}
147147
}
148148
}
149+
150+
func nopeVariadicNamedContainsFunc(slice []int) bool {
151+
for _, elem := range slice {
152+
if variadicPredicate(elem) {
153+
return true
154+
}
155+
}
156+
return false
157+
}
158+
159+
func variadicPredicate(int, ...any) bool
160+
161+
func nopeVariadicContainsFunc(slice []int) bool {
162+
f := func(int, ...any) bool {
163+
return true
164+
}
165+
for _, elem := range slice {
166+
if f(elem) {
167+
return true
168+
}
169+
}
170+
return false
171+
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,26 @@ func nopeNeedleHaystackDifferentTypes2(x error, args []any) {
102102
}
103103
}
104104
}
105+
106+
func nopeVariadicNamedContainsFunc(slice []int) bool {
107+
for _, elem := range slice {
108+
if variadicPredicate(elem) {
109+
return true
110+
}
111+
}
112+
return false
113+
}
114+
115+
func variadicPredicate(int, ...any) bool
116+
117+
func nopeVariadicContainsFunc(slice []int) bool {
118+
f := func(int, ...any) bool {
119+
return true
120+
}
121+
for _, elem := range slice {
122+
if f(elem) {
123+
return true
124+
}
125+
}
126+
return false
127+
}

0 commit comments

Comments
 (0)