Debian Codegraph @

go/analysis/passes/modernize/testdata/src/slicescontains/slicescontains.go.golden

raw
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 if slices.Contains(slice, needle) {
20 println("found")
21 }
22 }
23
24 func rangeValue(slice []int, needle int) {
25 if slices.Contains(slice, needle) {
26 println("found")
27 }
28 }
29
30 func returns(slice []int, needle int) {
31 if slices.Contains(slice, needle) {
32 println("found")
33 return
34 }
35 }
36
37 func assignTrueBreak(slice []int, needle int) {
38 found := slices.Contains(slice, needle)
39 print(found)
40 }
41
42 func assignFalseBreak(slice []int, needle int) {
43 found := !slices.Contains(slice, needle)
44 print(found)
45 }
46
47 func assignFalseBreakInSelectSwitch(slice []int, needle int) {
48 // Exercise RangeStmt in CommClause, CaseClause.
49 select {
50 default:
51 found := slices.Contains(slice, needle)
52 print(found)
53 }
54 switch {
55 default:
56 found := slices.Contains(slice, needle)
57 print(found)
58 }
59 }
60
61 func returnTrue(slice []int, needle int) bool {
62 return slices.Contains(slice, needle)
63 }
64
65 func returnFalse(slice []int, needle int) bool {
66 return !slices.Contains(slice, needle)
67 }
68
69 func containsFunc(slice []int, needle int) bool {
70 return slices.ContainsFunc(slice, predicate)
71 }
72
73 func nopeLoopBodyHasFreeContinuation(slice []int, needle int) bool {
74 for _, elem := range slice {
75 if predicate(elem) {
76 if needle == 7 {
77 continue // this statement defeats loop elimination
78 }
79 return true
80 }
81 }
82 return false
83 }
84
85 func generic[T any](slice []T, f func(T) bool) bool {
86 return slices.ContainsFunc(slice, f)
87 }
88
89 func predicate(int) bool
90
91 // Regression tests for bad fixes when needle
92 // and haystack have different types (#71313):
93
94 func nopeNeedleHaystackDifferentTypes(x any, args []error) {
95 for _, arg := range args {
96 if arg == x {
97 return
98 }
99 }
100 }
101
102 func nopeNeedleHaystackDifferentTypes2(x error, args []any) {
103 for _, arg := range args {
104 if arg == x {
105 return
106 }
107 }
108 }
109
110 func nopeVariadicNamedContainsFunc(slice []int) bool {
111 for _, elem := range slice {
112 if variadicPredicate(elem) {
113 return true
114 }
115 }
116 return false
117 }
118
119 func variadicPredicate(int, ...any) bool
120
121 func nopeVariadicContainsFunc(slice []int) bool {
122 f := func(int, ...any) bool {
123 return true
124 }
125 for _, elem := range slice {
126 if f(elem) {
127 return true
128 }
129 }
130 return false
131 }
132
133 // Negative test case for implicit C->I conversion
134 type I interface{ F() }
135 type C int
136
137 func (C) F() {}
138
139 func nopeImplicitConversionContainsFunc(slice []C, f func(I) bool) bool {
140 for _, elem := range slice {
141 if f(elem) { // implicit conversion from C to I
142 return true
143 }
144 }
145 return false
146 }
147
148 func nopeTypeParamWidening[T any](slice []T, f func(any) bool) bool {
149 for _, elem := range slice {
150 if f(elem) { // implicit conversion from T to any
151 return true
152 }
153 }
154 return false
155 }
156
157 func issue76210(haystack []string, needle string) bool {
158 res := len(haystack) == 0
159 if slices.Contains(haystack, needle) {
160 res = true
161 }
162 return res
163 }
164
165 func issue76210negation(haystack []string, needle string) bool {
166 res := len(haystack) != 0
167 if slices.Contains(haystack, needle) {
168 res = false
169 }
170 return res
171 }
172
173 func issue77677emptybody(slice []int, needle int) {
174 for _, elem := range slice { // nope: would produce "if slices.Contains" with an empty body
175 if elem == needle {
176 break
177 }
178 }
179 }
180
181 type Object struct{}
182
183 func (o Object) Do() Object { return Object{} }
184 func (o Object) Print() { fmt.Println("Hello, World!") }
185
186 func issue78149nonboolassign(obj Object, opts ...string) {
187 o := obj
188 if slices.Contains(opts, "something") {
189 o = o.Do()
190 }
191 o.Print()
192 }