| 1 |
package slicescontains |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"slices" |
| 6 |
) |
| 7 |
|
| 8 |
var _ = slices.Contains[[]int] // force import of "slices" to avoid duplicate import edits |
| 9 |
|
| 10 |
func nopeNoBreak(slice []int, needle int) { |
| 11 |
for i := range slice { |
| 12 |
if slice[i] == needle { |
| 13 |
println("found") |
| 14 |
} |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
func rangeIndex(slice []int, needle int) { |
| 19 |
for i := range slice { // want "Loop can be simplified using slices.Contains" |
| 20 |
if slice[i] == needle { |
| 21 |
println("found") |
| 22 |
break |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func rangeValue(slice []int, needle int) { |
| 28 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 29 |
if elem == needle { |
| 30 |
println("found") |
| 31 |
break |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func returns(slice []int, needle int) { |
| 37 |
for i := range slice { // want "Loop can be simplified using slices.Contains" |
| 38 |
if slice[i] == needle { |
| 39 |
println("found") |
| 40 |
return |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func assignTrueBreak(slice []int, needle int) { |
| 46 |
found := false |
| 47 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 48 |
if elem == needle { |
| 49 |
found = true |
| 50 |
break |
| 51 |
} |
| 52 |
} |
| 53 |
print(found) |
| 54 |
} |
| 55 |
|
| 56 |
func assignFalseBreak(slice []int, needle int) { |
| 57 |
found := true |
| 58 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 59 |
if elem == needle { |
| 60 |
found = false |
| 61 |
break |
| 62 |
} |
| 63 |
} |
| 64 |
print(found) |
| 65 |
} |
| 66 |
|
| 67 |
func assignFalseBreakInSelectSwitch(slice []int, needle int) { |
| 68 |
// Exercise RangeStmt in CommClause, CaseClause. |
| 69 |
select { |
| 70 |
default: |
| 71 |
found := false |
| 72 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 73 |
if elem == needle { |
| 74 |
found = true |
| 75 |
break |
| 76 |
} |
| 77 |
} |
| 78 |
print(found) |
| 79 |
} |
| 80 |
switch { |
| 81 |
default: |
| 82 |
found := false |
| 83 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 84 |
if elem == needle { |
| 85 |
found = true |
| 86 |
break |
| 87 |
} |
| 88 |
} |
| 89 |
print(found) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func returnTrue(slice []int, needle int) bool { |
| 94 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 95 |
if elem == needle { |
| 96 |
return true |
| 97 |
} |
| 98 |
} |
| 99 |
return false |
| 100 |
} |
| 101 |
|
| 102 |
func returnFalse(slice []int, needle int) bool { |
| 103 |
for _, elem := range slice { // want "Loop can be simplified using slices.Contains" |
| 104 |
if elem == needle { |
| 105 |
return false |
| 106 |
} |
| 107 |
} |
| 108 |
return true |
| 109 |
} |
| 110 |
|
| 111 |
func containsFunc(slice []int, needle int) bool { |
| 112 |
for _, elem := range slice { // want "Loop can be simplified using slices.ContainsFunc" |
| 113 |
if predicate(elem) { |
| 114 |
return true |
| 115 |
} |
| 116 |
} |
| 117 |
return false |
| 118 |
} |
| 119 |
|
| 120 |
func nopeLoopBodyHasFreeContinuation(slice []int, needle int) bool { |
| 121 |
for _, elem := range slice { |
| 122 |
if predicate(elem) { |
| 123 |
if needle == 7 { |
| 124 |
continue // this statement defeats loop elimination |
| 125 |
} |
| 126 |
return true |
| 127 |
} |
| 128 |
} |
| 129 |
return false |
| 130 |
} |
| 131 |
|
| 132 |
func generic[T any](slice []T, f func(T) bool) bool { |
| 133 |
for _, elem := range slice { // want "Loop can be simplified using slices.ContainsFunc" |
| 134 |
if f(elem) { |
| 135 |
return true |
| 136 |
} |
| 137 |
} |
| 138 |
return false |
| 139 |
} |
| 140 |
|
| 141 |
func predicate(int) bool |
| 142 |
|
| 143 |
// Regression tests for bad fixes when needle |
| 144 |
// and haystack have different types (#71313): |
| 145 |
|
| 146 |
func nopeNeedleHaystackDifferentTypes(x any, args []error) { |
| 147 |
for _, arg := range args { |
| 148 |
if arg == x { |
| 149 |
return |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func nopeNeedleHaystackDifferentTypes2(x error, args []any) { |
| 155 |
for _, arg := range args { |
| 156 |
if arg == x { |
| 157 |
return |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func nopeVariadicNamedContainsFunc(slice []int) bool { |
| 163 |
for _, elem := range slice { |
| 164 |
if variadicPredicate(elem) { |
| 165 |
return true |
| 166 |
} |
| 167 |
} |
| 168 |
return false |
| 169 |
} |
| 170 |
|
| 171 |
func variadicPredicate(int, ...any) bool |
| 172 |
|
| 173 |
func nopeVariadicContainsFunc(slice []int) bool { |
| 174 |
f := func(int, ...any) bool { |
| 175 |
return true |
| 176 |
} |
| 177 |
for _, elem := range slice { |
| 178 |
if f(elem) { |
| 179 |
return true |
| 180 |
} |
| 181 |
} |
| 182 |
return false |
| 183 |
} |
| 184 |
|
| 185 |
// Negative test case for implicit C->I conversion |
| 186 |
type I interface{ F() } |
| 187 |
type C int |
| 188 |
|
| 189 |
func (C) F() {} |
| 190 |
|
| 191 |
func nopeImplicitConversionContainsFunc(slice []C, f func(I) bool) bool { |
| 192 |
for _, elem := range slice { |
| 193 |
if f(elem) { // implicit conversion from C to I |
| 194 |
return true |
| 195 |
} |
| 196 |
} |
| 197 |
return false |
| 198 |
} |
| 199 |
|
| 200 |
func nopeTypeParamWidening[T any](slice []T, f func(any) bool) bool { |
| 201 |
for _, elem := range slice { |
| 202 |
if f(elem) { // implicit conversion from T to any |
| 203 |
return true |
| 204 |
} |
| 205 |
} |
| 206 |
return false |
| 207 |
} |
| 208 |
|
| 209 |
func issue76210(haystack []string, needle string) bool { |
| 210 |
res := len(haystack) == 0 |
| 211 |
for _, elem := range haystack { // want "Loop can be simplified using slices.Contains" |
| 212 |
if needle == elem { |
| 213 |
res = true |
| 214 |
break |
| 215 |
} |
| 216 |
} |
| 217 |
return res |
| 218 |
} |
| 219 |
|
| 220 |
func issue76210negation(haystack []string, needle string) bool { |
| 221 |
res := len(haystack) != 0 |
| 222 |
for _, elem := range haystack { // want "Loop can be simplified using slices.Contains" |
| 223 |
if needle == elem { |
| 224 |
res = false |
| 225 |
break |
| 226 |
} |
| 227 |
} |
| 228 |
return res |
| 229 |
} |
| 230 |
|
| 231 |
func issue77677emptybody(slice []int, needle int) { |
| 232 |
for _, elem := range slice { // nope: would produce "if slices.Contains" with an empty body |
| 233 |
if elem == needle { |
| 234 |
break |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
type Object struct{} |
| 240 |
|
| 241 |
func (o Object) Do() Object { return Object{} } |
| 242 |
func (o Object) Print() { fmt.Println("Hello, World!") } |
| 243 |
|
| 244 |
func issue78149nonboolassign(obj Object, opts ...string) { |
| 245 |
o := obj |
| 246 |
for _, opt := range opts { // want "Loop can be simplified using slices.Contains" |
| 247 |
if opt == "something" { |
| 248 |
o = o.Do() |
| 249 |
break |
| 250 |
} |
| 251 |
} |
| 252 |
o.Print() |
| 253 |
} |