Skip to content

Commit 0539a50

Browse files
committed
Allow more cuddling with with go routines
Don't be as strict with cuddling go stmts. A lot of the time a go routine will be called on a type called or assigned on the line above or on an assignment on the line above will be used in the go routine call.
1 parent 3e0a4f4 commit 0539a50

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

wsl.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ func (p *Processor) ProcessFiles(filenames []string) ([]Result, []string) {
247247
func (p *Processor) process(filename string, data []byte) {
248248
fileSet := token.NewFileSet()
249249
file, err := parser.ParseFile(fileSet, filename, data, parser.ParseComments)
250-
251250
// If the file is not parsable let's add a syntax error and move on.
252251
if err != nil {
253252
p.result = append(p.result, Result{
@@ -335,7 +334,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
335334
var calledOnLineAbove []string
336335

337336
// Check if the previous statement spans over multiple lines.
338-
var cuddledWithMultiLineAssignment = cuddledWithLastStmt && p.nodeStart(previousStatement) != p.nodeStart(stmt)-1
337+
cuddledWithMultiLineAssignment := cuddledWithLastStmt && p.nodeStart(previousStatement) != p.nodeStart(stmt)-1
339338

340339
// Ensure previous line is not a multi line assignment and if not get
341340
// rightAndLeftHandSide assigned variables.
@@ -672,6 +671,22 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
672671
continue
673672
}
674673

674+
if c, ok := t.Call.Fun.(*ast.SelectorExpr); ok {
675+
goCallArgs := append(p.findLHS(c.X), p.findRHS(c.X)...)
676+
677+
if atLeastOneInListsMatch(calledOnLineAbove, goCallArgs) {
678+
continue
679+
}
680+
}
681+
682+
if c, ok := t.Call.Fun.(*ast.FuncLit); ok {
683+
goCallArgs := append(p.findLHS(c.Body), p.findRHS(c.Body)...)
684+
685+
if atLeastOneInListsMatch(assignedOnLineAbove, goCallArgs) {
686+
continue
687+
}
688+
}
689+
675690
if !atLeastOneInListsMatch(rightAndLeftHandSide, assignedOnLineAbove) {
676691
p.addError(t.Pos(), reasonGoFuncWithoutAssign)
677692
}
@@ -1207,7 +1222,7 @@ func (p *Processor) nodeStart(node ast.Node) int {
12071222
}
12081223

12091224
func (p *Processor) nodeEnd(node ast.Node) int {
1210-
var line = p.fileSet.Position(node.End()).Line
1225+
line := p.fileSet.Position(node.End()).Line
12111226

12121227
if isEmptyLabeledStmt(node) {
12131228
return p.fileSet.Position(node.Pos()).Line

wsl_test.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,59 @@ func TestShouldAddEmptyLines(t *testing.T) {
12311231
}()
12321232
}`),
12331233
},
1234+
{
1235+
description: "go stmt can be cuddled if used in call",
1236+
code: []byte(`package main
1237+
1238+
func main() {
1239+
wg := sync.WaitGroup{}
1240+
go func(wg) {
1241+
defer wg.Done()
1242+
1243+
fmt.Println("cuddling correctly")
1244+
}()
1245+
1246+
one := 1
1247+
go func(wg) {
1248+
defer wg.Done()
1249+
1250+
fmt.Println("cuddling faulty")
1251+
}()
1252+
1253+
two := 2
1254+
go someFunc(two)
1255+
1256+
wg.Wait()
1257+
1258+
_ = one
1259+
_ = two
1260+
}`),
1261+
expectedErrorStrings: []string{"go statements can only invoke functions assigned on line above"},
1262+
},
1263+
{
1264+
description: "calling go stmt on type used above should be ok",
1265+
code: []byte(`package main
1266+
1267+
type A struct {
1268+
wg sync.WaitGrop
1269+
}
1270+
1271+
func (a *A) doWork() {
1272+
fmt.Println("doing work")
1273+
a.wg.Done()
1274+
}
1275+
1276+
func main() {
1277+
a := A{
1278+
wg: sync.WaitGroup{},
1279+
}
1280+
1281+
a.wg.Add(1)
1282+
go a.doWork()
1283+
1284+
wg.Done()
1285+
}`),
1286+
},
12341287
}
12351288

12361289
for _, tc := range cases {
@@ -1574,7 +1627,7 @@ func TestWithConfig(t *testing.T) {
15741627
once.Do(func() {
15751628
err = ProduceError()
15761629
})
1577-
1630+
15781631
if err != nil {
15791632
return nil, err
15801633
}
@@ -1905,7 +1958,7 @@ func TestWithConfig(t *testing.T) {
19051958
c := 3
19061959
b = 2
19071960
1908-
// should fail
1961+
// should fail
19091962
err := DoSomething()
19101963
if err != nil {
19111964
b = 4

0 commit comments

Comments
 (0)