summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mgechev/revive/rule/waitgroup-by-value.go
blob: b86929136cb7911d36498f9aab720f3417120d9f (plain)
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
package rule

import (
	"go/ast"

	"github.com/mgechev/revive/lint"
)

// WaitGroupByValueRule lints sync.WaitGroup passed by copy in functions.
type WaitGroupByValueRule struct{}

// Apply applies the rule to given file.
func (r *WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
	var failures []lint.Failure

	onFailure := func(failure lint.Failure) {
		failures = append(failures, failure)
	}

	w := lintWaitGroupByValueRule{onFailure: onFailure}
	ast.Walk(w, file.AST)
	return failures
}

// Name returns the rule name.
func (r *WaitGroupByValueRule) Name() string {
	return "waitgroup-by-value"
}

type lintWaitGroupByValueRule struct {
	onFailure func(lint.Failure)
}

func (w lintWaitGroupByValueRule) Visit(node ast.Node) ast.Visitor {
	// look for function declarations
	fd, ok := node.(*ast.FuncDecl)
	if !ok {
		return w
	}

	// Check all function's parameters
	for _, field := range fd.Type.Params.List {
		if !w.isWaitGroup(field.Type) {
			continue
		}

		w.onFailure(lint.Failure{
			Confidence: 1,
			Node:       field,
			Failure:    "sync.WaitGroup passed by value, the function will get a copy of the original one",
		})
	}

	return nil
}

func (lintWaitGroupByValueRule) isWaitGroup(ft ast.Expr) bool {
	se, ok := ft.(*ast.SelectorExpr)
	if !ok {
		return false
	}

	x, _ := se.X.(*ast.Ident)
	sel := se.Sel.Name
	return x.Name == "sync" && sel == "WaitGroup"
}