@@ -11,50 +11,45 @@ import (
11
11
type DataRaceRule struct {}
12
12
13
13
// Apply applies the rule to given file.
14
- func (* DataRaceRule ) Apply (file * lint.File , _ lint.Arguments ) []lint.Failure {
14
+ func (r * DataRaceRule ) Apply (file * lint.File , _ lint.Arguments ) []lint.Failure {
15
+ isGo122 := file .Pkg .IsAtLeastGo122 ()
15
16
var failures []lint.Failure
16
- onFailure := func (failure lint.Failure ) {
17
- failures = append (failures , failure )
18
- }
19
- w := lintDataRaces {onFailure : onFailure , go122for : file .Pkg .IsAtLeastGo122 ()}
17
+ for _ , decl := range file .AST .Decls {
18
+ funcDecl , ok := decl .(* ast.FuncDecl )
19
+ if ! ok || funcDecl .Body == nil {
20
+ continue // not function declaration or empty function
21
+ }
20
22
21
- ast . Walk ( w , file . AST )
23
+ funcResults := funcDecl . Type . Results
22
24
23
- return failures
24
- }
25
+ returnIDs := map [* ast.Object ]struct {}{}
26
+ if funcResults != nil {
27
+ returnIDs = r .ExtractReturnIDs (funcResults .List )
28
+ }
25
29
26
- // Name returns the rule name.
27
- func (* DataRaceRule ) Name () string {
28
- return "datarace"
29
- }
30
+ onFailure := func (failure lint.Failure ) {
31
+ failures = append (failures , failure )
32
+ }
30
33
31
- type lintDataRaces struct {
32
- onFailure func (failure lint.Failure )
33
- go122for bool
34
- }
34
+ fl := & lintFunctionForDataRaces {
35
+ onFailure : onFailure ,
36
+ returnIDs : returnIDs ,
37
+ rangeIDs : map [* ast.Object ]struct {}{},
38
+ go122for : isGo122 ,
39
+ }
35
40
36
- func (w lintDataRaces ) Visit (n ast.Node ) ast.Visitor {
37
- node , ok := n .(* ast.FuncDecl )
38
- if ! ok {
39
- return w // not function declaration
41
+ ast .Walk (fl , funcDecl .Body )
40
42
}
41
- if node .Body == nil {
42
- return nil // empty body
43
- }
44
-
45
- results := node .Type .Results
46
43
47
- returnIDs := map [* ast.Object ]struct {}{}
48
- if results != nil {
49
- returnIDs = w .ExtractReturnIDs (results .List )
50
- }
51
- fl := & lintFunctionForDataRaces {onFailure : w .onFailure , returnIDs : returnIDs , rangeIDs : map [* ast.Object ]struct {}{}, go122for : w .go122for }
52
- ast .Walk (fl , node .Body )
44
+ return failures
45
+ }
53
46
54
- return nil
47
+ // Name returns the rule name.
48
+ func (* DataRaceRule ) Name () string {
49
+ return "datarace"
55
50
}
56
51
57
- func (lintDataRaces ) ExtractReturnIDs (fields []* ast.Field ) map [* ast.Object ]struct {} {
52
+ func (* DataRaceRule ) ExtractReturnIDs (fields []* ast.Field ) map [* ast.Object ]struct {} {
58
53
r := map [* ast.Object ]struct {}{}
59
54
for _ , f := range fields {
60
55
for _ , id := range f .Names {
0 commit comments