1package testdata
2
3// Cases where short syntax SHOULD be used AND IS used.
4
5func used_CondBinary_UsedInBody_OK() {
6	if v := getValue(); v != nil {
7		noOp1(v)
8	}
9}
10
11func used_CondBinary_UsedInElse_OK() {
12	if v := getValue(); v != nil {
13	} else {
14		noOp2(v)
15	}
16}
17
18func used_CondBinary_UsedInBodyAndElse_OK() {
19	if v := getValue(); v != nil {
20		noOp1(v)
21	} else {
22		noOp2(v)
23	}
24}
25
26// Cases where short syntax SHOULD be used BUT is NOT used.
27
28func notUsed_CondBinaryExpr_NotOK() {
29	v := getValue() // want "variable '.+' is only used in the if-statement"
30	if v != nil {
31		noOp1(v)
32	}
33}
34
35func notUsed_CondCallExpr_NotOK() {
36	a := getValue() // want "variable '.+' is only used in the if-statement"
37	if getBool(a) {
38	}
39
40	b := getValue() // want "variable '.+' is only used in the if-statement"
41	if getBool(getValue(b)) {
42	}
43}
44
45func notUsed_Body_NotOK() {
46	v := getValue() // want "variable '.+' is only used in the if-statement"
47	if true {
48		noOp1(v)
49	}
50}
51
52func notUsed_Else_NotOK() {
53	v := getValue() // want "variable '.+' is only used in the if-statement"
54	if true {
55	} else {
56		noOp2(v)
57	}
58}
59
60func notUsed_DifferentVarsWithSameName_NotOK() {
61	_, b := getTwoValues() // want "variable '.+' is only used in the if-statement"
62	if b != nil {
63		noOp1(b)
64	}
65
66	a, b := getTwoValues()
67	if b != nil {
68		noOp1(a)
69	}
70	noOp2(b)
71}
72
73// Cases where short syntax SHOULD NOT be used AND IS NOT used.
74
75func notUsed_DeferStmt_OK() {
76	v := getValue()
77	if v != nil {
78		noOp1(v)
79	}
80	defer noOp2(v)
81}
82
83func notUsed_IfStmt_CondBinaryExpr_OK() {
84	v := getValue()
85	if v != nil {
86		noOp1(v)
87	}
88	if v == nil {
89		noOp2(v)
90	}
91}
92
93func notUsed_IfStmt_CondBinaryExpr_MethodCall_OK() {
94	dt := getDummy()
95	if v := dt.getValue(); v == nil {
96		noOp1(v)
97	}
98	if dt.v != nil {
99		noOp2(dt)
100	}
101}
102
103func notUsed_IfStmt_CondCallExpr_OK() {
104	v := getValue()
105	if v != nil {
106		noOp1(v)
107	}
108	if getValue(v) != nil {
109		noOp2(v)
110	}
111}
112
113func notUsed_IfStmt_Body_OK() {
114	a, b := 0, 0
115
116	if a <= 0 {
117		noOp1(a)
118	} else if a > 0 {
119		noOp2(a)
120	}
121	if b > 0 {
122		noOp1(a)
123		for a < 0 {
124			noOp2(a)
125			a++
126		}
127		noOp1(a)
128	}
129}
130
131func notUsed_IfStmt_AssignInLeftHandSide_OK() {
132	a := 0
133
134	if b := 0; b > 0 {
135		a = 0
136	}
137	if a > 0 {
138		return
139	}
140}
141
142func notUsed_GoStmt_OK() {
143	v := getValue()
144	if v != nil {
145		noOp1(v)
146	}
147	go noOp2(v)
148}
149
150func notUsed_ReturnStmt_OK() interface{} {
151	v := getValue()
152	if v != nil {
153		noOp1(v)
154	}
155	return v
156}
157
158func notUsed_SendStmt_OK() {
159	v := getValue()
160	if v != nil {
161		noOp1(v)
162	}
163
164	vChan := make(chan interface{}, 1)
165	vChan <- v
166}
167
168func notUsed_SwitchStmt_Tag_OK() {
169	v := getValue()
170	if v != nil {
171		noOp1(v)
172	}
173	switch v {
174	case nil:
175	}
176}
177
178func notUsed_SwitchStmt_CaseList_OK() {
179	v := getValue()
180	if v != nil {
181		noOp1(v)
182	}
183	switch {
184	case v == nil:
185	}
186}
187
188func notUsed_SwitchStmt_CaseBody_OK() {
189	v := getValue()
190	if v != nil {
191		noOp1(v)
192	}
193	switch {
194	case true:
195		noOp2(v)
196	}
197}
198
199func notUsed_SwitchStmt_Body_OK() {
200	a := getValue()
201	if a != nil {
202		noOp1(a)
203	}
204	b := getValue()
205	switch b {
206	case a:
207	}
208}
209
210func notUsed_SwitchStmt_Body_Assignment_OK() {
211	size := 0
212	if size == 0 {
213		return
214	}
215
216	switch 0 {
217	case 0:
218		a := make([]byte, size-2)
219		noOp1(a)
220	}
221}
222
223func notUsed_CompositeLiteral_OK() map[int]struct{} {
224	a := 0
225	if a != 0 {
226		return nil
227	}
228
229	b := struct{}{}
230
231	return map[int]struct{}{a: b}
232}
233
234func notUsed_MultipleAssignments_OK() interface{} {
235	a, b := getTwoValues()
236	if a != nil {
237		return a
238	}
239	return b
240}
241
242func notUsed_MultipleAssignments_AllUsesInIfs_OK() interface{} {
243	a, b := getTwoValues()
244	if a != nil {
245		return a
246	}
247	if b != nil {
248		return b
249	}
250	return nil
251}
252
253func notUsed_MultipleAssignments_WhenFlagSettingsAreNotSatisfied_OK() {
254	longDeclarationToDissatisfyFlagSettings, b := getTwoValues()
255	if b != nil {
256		return
257	}
258
259	c := getValue(longDeclarationToDissatisfyFlagSettings)
260	noOp1(c)
261}
262
263func notUsed_LongDecl_OK() {
264	v := getValue("Long long long long long declaration, linter shouldn't force short syntax for it, at least I hope so.")
265	if v != nil {
266		noOp1(v)
267	}
268}
269
270func notUsed_HighDecl_OK() {
271	v := getValue(
272		nil,
273		nil,
274		nil,
275	)
276	if v != nil {
277		noOp1(v)
278	}
279}
280
281func notUsed_MethodCall_OK() {
282	d := dummyType{}
283	if d.v == nil {
284	}
285	d.noOp()
286}
287
288func notUsed_MethodCallWithAssignment_OK() {
289	d := dummyType{}
290	if d.v != nil {
291	}
292
293	v := d.getValue()
294	noOp1(v)
295}
296
297func notUsed_MethodCall_Nested_OK() {
298	d := dummyType{}
299	if d.v != nil {
300	}
301	noOp1(d.getValue())
302}
303
304func notUsed_Pointer_OK() {
305	d := dummyType{}
306	if d.v != nil {
307	}
308	noOp1(&d)
309}
310
311func notUsed_CondMethodCall_OK() {
312	d := dummyType{}
313	if d.getValue() == nil {
314	}
315}
316
317func notUsed_Range_OK() {
318	ds := []dummyType{}
319	if ds == nil {
320	}
321
322	for _, d := range ds {
323		d.noOp()
324	}
325}
326
327func notUsed_ForCond_OK(i int) {
328	for i < 0 {
329		break
330	}
331	if i == 0 {
332		return
333	}
334}
335
336func notUsed_ForBody_OK(t int) {
337	d := getDummy()
338	if d.v == nil {
339		return
340	}
341
342	for i := 0; i < t; i++ {
343		noOp1(d.v)
344	}
345}
346
347func notUsed_ForPost_OK() {
348	i := 0
349	for ; ; i++ {
350		break
351	}
352
353	if i == 0 {
354		return
355	}
356}
357
358func notUsed_IncrementDecrement_OK() {
359	i := 0
360	i++
361	if i == 0 {
362		return
363	}
364	i--
365}
366
367func notUsed_AssignToField_OK() {
368	d := dummyType{}
369	d.v = getValue()
370}
371
372func notUsed_ReferenceToFields_OK() {
373	a, b := getTwoDummies()
374	if getBool(a.v) {
375		return
376	}
377	noOp1(b.v)
378}
379
380func notUsed_IndexExpression_Index_OK() {
381	s := []int{}
382
383	length := len(s)
384	if length == 0 {
385		return
386	}
387
388	last := s[length-1]
389	noOp1(last)
390}
391
392func notUsed_IndexExpression_Indexed_OK() {
393	s := []int{}
394	if s == nil {
395		return
396	}
397
398	first := s[0]
399	noOp1(first)
400}
401
402func notUsed_BinaryExpressionInIndex_OK(size int) {
403	if size == 0 {
404		return
405	}
406
407	a := make([]byte, size-1)
408	noOp1(a)
409}
410
411func notUsed_SliceExpression_Low_OK(size int) {
412	s := []int{}
413	if size != 0 {
414		return
415	}
416	noOp2(s[size:])
417}
418
419func notUsed_SliceExpression_High_OK(size int) {
420	s := []int{}
421	if size != 0 {
422		return
423	}
424	noOp1(s[:size])
425}
426
427func notUsed_FuncLitReturn_OK() {
428	s := ""
429	tp := func() string { return s }
430
431	tp()
432
433	if s != "" {
434		return
435	}
436}
437