Debian Codegraph @

staticcheck/sa4010/testdata/go1.0/CheckIneffectiveAppend/CheckIneffectiveAppend.go

raw
1 package pkg
2
3 import "fmt"
4
5 func fn1() {
6 var s []int
7 s = append(s, 1) //@ diag(`this result of append is never used`)
8 s = append(s, 1) //@ diag(`this result of append is never used`)
9 }
10
11 func fn2() (named []int) {
12 named = append(named, 1)
13 return
14 }
15
16 func fn3() {
17 s := make([]int, 0)
18 s = append(s, 1) //@ diag(`this result of append is never used`)
19 }
20
21 func fn3_1(n int) {
22 s := make([]int, n)
23 s = append(s, 1) //@ diag(`this result of append is never used`)
24 }
25
26 func fn4() []int {
27 var s []int
28 s = append(s, 1)
29 return s
30 }
31
32 func fn5() {
33 var s []int
34 s = append(s, 1)
35 fn6(s)
36 }
37
38 func fn6([]int) {}
39
40 func fn7() {
41 var s []int
42 fn8(&s)
43 s = append(s, 1)
44 }
45
46 func fn8(*[]int) {}
47
48 func fn9() {
49 var s []int
50 s = append(s, 1)
51 fmt.Println(s)
52 s = append(s, 1)
53 }
54
55 func fn10() {
56 var s []int
57 return
58 s = append(s, 1)
59 }
60
61 func fn11() {
62 var s []int
63 for x := 0; x < 10; x++ {
64 s = append(s, 1) //@ diag(`this result of append is never used`)
65 }
66 }
67
68 func fn12(a []int) {
69 b := a[:0]
70 for _, x := range a {
71 if true {
72 b = append(b, x)
73 }
74 }
75 }
76
77 func fn13() []byte {
78 a := make([]byte, 10)
79 b := a[:5]
80 for i := 0; i < 2; i++ {
81 a = append(a, 1)
82 }
83 return b
84 }
85
86 func fn14() []byte {
87 a := make([]byte, 10)
88 b := a[:5]
89 for i := 0; i < 2; i++ {
90 b = append(b, 1)
91 }
92 return a
93 }
94
95 func fn15() {
96 s := make([]byte, 0, 1)
97 retain(s)
98 s = append(s, 1)
99 }
100
101 func fn16(s []byte) {
102 for i := 0; i < 2; i++ {
103 s = append(s, 1)
104 }
105 }
106
107 func fn17(x *[5]byte) {
108 s := x[:0]
109 for i := 0; i < 2; i++ {
110 s = append(s, 1)
111 }
112 }
113
114 func fn18() {
115 var x [4]byte
116 s := x[:0]
117 for i := 0; i < 2; i++ {
118 s = append(s, 1)
119 }
120 _ = x
121 }
122
123 func fn19() [4]int {
124 var x [4]int
125 s := x[:]
126 s = append(s, 1)
127 return x
128 }
129
130 func fn20() {
131 var x [4]int
132 s := x[:]
133 s = append(s, 1) //@ diag(`this result of append is never used`)
134 }
135
136 func fn20_1() {
137 var x [4]int
138 s := x[:]
139 s = append(s, 1)
140 _ = x[0]
141 }
142
143 func fn21() {
144 var x []byte
145 x = append(x, 1)
146 retain(x)
147 x = append(x, 2)
148 }
149
150 func fn22_1() {
151 var x1 []byte
152 x2 := append(x1, 1) //@ diag(`this result of append is never used`)
153 x2 = append(x2, 2) //@ diag(`this result of append is never used`)
154 x3 := append(x1, 3) //@ diag(`this result of append is never used`)
155 x3 = append(x3, 4) //@ diag(`this result of append is never used`)
156 }
157
158 func fn23(n int) []int {
159 s := make([]int, 0, n)
160 s2 := append(s, 1, 2, 3) // this can be observed by extending the capacity of x
161 s2 = append(s2, 4)
162 x := append(s, 2)
163 return x
164 }
165
166 func fn24() []byte {
167 x := make([]byte, 0, 24)
168 s1 := append(x, 1)
169 s2 := append(s1, 2)
170 s2 = append(s2, 3)
171 s3 := append(s1, 4)
172 return s3
173 }
174
175 func fn25() {
176 var s []byte
177 if true {
178 s = append(s, 1) //@ diag(`this result of append is never used`)
179 }
180 s = append(s, 2) //@ diag(`this result of append is never used`)
181 }
182
183 func fn26() {
184 var s []byte
185 if true {
186 s = append(s, 1)
187 retain(s)
188 }
189 s = append(s, 2)
190 }
191
192 func fn27() {
193 var s []byte
194 if true {
195 s = make([]byte, 0, 1)
196 } else {
197 s = make([]byte, 0, 2)
198 }
199 s = append(s, 1) //@ diag(`this result of append is never used`)
200 }
201
202 func fn28() {
203 var s []byte
204 if true {
205 s = make([]byte, 0, 1)
206 } else {
207 s = make([]byte, 0, 2)
208 retain(s)
209 }
210 s = append(s, 1)
211 }
212
213 func fn29() {
214 x := gen()
215 x = append(x, 1)
216 }
217
218 func fn30(x T) {
219 s := x.s
220 s = append(s, 1)
221 }
222
223 var Global []int
224
225 func fn31() {
226 Global = append(Global, 1)
227 }
228
229 type T struct {
230 s []byte
231 }
232
233 func gen() []byte { return nil }
234
235 func retain([]byte) {}