File: wsl.go

package info (click to toggle)
gitaly 16.11.6%2Bds1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,623,960 kB
  • sloc: cpp: 33,732; java: 20,579; sh: 5,372; cs: 3,973; javascript: 3,426; python: 3,053; makefile: 2,303; ansic: 1,729; php: 1,455; asm: 1,217; xml: 838; ruby: 452; sql: 431; yacc: 22; sed: 1
file content (219 lines) | stat: -rw-r--r-- 3,928 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//golangcitest:args -Ewsl
//golangcitest:config_path testdata/configs/wsl.yml
package testdata

import (
	"context"
	"fmt"
)

func main() {
	var (
		y = 0
	)
	if y < 1 { // want "if statements should only be cuddled with assignments"
		fmt.Println("tight")
	}

	thisIsNotUsedInIf := true
	if 2 > 1 { // want "if statements should only be cuddled with assignments used in the if statement itself"
		return
	}

	one := 1
	two := 2
	three := 3
	if three == 3 { // want "only one cuddle assignment allowed before if statement"
		fmt.Println("too many cuddled assignments", one, two, thisIsNotUsedInIf)
	}

	var a = "a"
	var b = "b" // want "declarations should never be cuddled"

	if true {
		return
	}
	if false { // want "if statements should only be cuddled with assignments"
		return
	}

	for i := range make([]int, 10) {
		fmt.Println(i)
		fmt.Println(i + i)
		continue // want "branch statements should not be cuddled if block has more than two lines"
	}

	assignOne := a
	fmt.Println(assignOne)
	assignTwo := b // want "assignments should only be cuddled with other assignments"
	fmt.Println(assignTwo)

	_, cf1 := context.WithCancel(context.Background())
	_, cf2 := context.WithCancel(context.Background())
	defer cf1() // want "only one cuddle assignment allowed before defer statement"
	defer cf2()

	err := multiline(
		"spanning",
		"multiple",
	)
	if err != nil {
		panic(err)
	}

	notErr := multiline(
		"spanning",
		"multiple",
	)
	if err != nil { // want "if statements should only be cuddled with assignments used in the if statement itself"
		panic("not from the line above")
	}

	// This is OK since we use a variable from the line above, even if we don't
	// check it with the if.
	xx := notErr
	if err != nil {
		panic(xx)
	}
}

func multiline(s ...string) error {
	return nil
}

func f1() int {
	x := 1
	return x
}

func f2() int {
	x := 1
	y := 3
	return x + y // want "return statements should not be cuddled if block has more than two lines"
}

func f3() int {
	sum := 0
	for _, v := range []int{2, 4, 8} {
		sum += v
	}

	notSum := 0
	for _, v := range []int{1, 2, 4} { // want "ranges should only be cuddled with assignments used in the iteration"
		sum += v
	}

	return sum + notSum
}

func onelineShouldNotError() error { return nil }

func multilineCase() {
	// Multiline cases
	switch {
	case true,
		false:
		fmt.Println("ok")
	case false ||
		true:
		fmt.Println("ok")
	case true,
		false:
		fmt.Println("ok")
	}
}

func sliceExpr() {
	// Index- and slice expressions.
	var aSlice = []int{1, 2, 3}

	start := 2
	if v := aSlice[start]; v == 1 {
		fmt.Println("ok")
	}

	notOk := 1
	if v := aSlice[start]; v == 1 { // want "if statements should only be cuddled with assignments used in the if statement itself"
		fmt.Println("notOk")
		fmt.Println(notOk)
	}

	end := 2
	if len(aSlice[start:end]) > 2 {
		fmt.Println("ok")
	}
}

func indexExpr() {
	var aMap = map[string]struct{}{"key": {}}

	key := "key"
	if k, ok := aMap[key]; ok {
		fmt.Println(k)
	}

	xxx := "xxx"
	if _, ok := aMap[key]; ok { // want "if statements should only be cuddled with assignments used in the if statement itself"
		fmt.Println("not ok")
		fmt.Println(xxx)
	}
}

func allowTrailing(i int) {
	switch i {
	case 1:
		fmt.Println("one")

	case 2:
		fmt.Println("two")
		// Comments OK too!
	case 3:
		fmt.Println("three")
	}
}

// ExampleSomeOutput simulates an example function.
func ExampleSomeOutput() {
	fmt.Println("Hello, world")

	// Output:
	// Hello, world
}

func IncDecStmt() {
	counter := 0
	for range make([]int, 5) {
		counter++
	}

	type t struct {
		counter int
	}

	x := t{5}

	x.counter--
	if x.counter > 0 {
		fmt.Println("not yet 0")
	}
}

func AnonymousBlock() {
	func(a, b int) { // want "block should not start with a whitespace"

		fmt.Println(a + b)
	}(1, 1)
}

func MultilineComment() {
	if true {
		/*
			Ok to start block with
			a
			long
			multiline
			cmoment
		*/
		fmt.Println("true")
	}
}