-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsub_analyzer.go
More file actions
43 lines (39 loc) · 1.75 KB
/
Copy pathsub_analyzer.go
File metadata and controls
43 lines (39 loc) · 1.75 KB
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
package mutex
import (
"reflect"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/common/commentfilter"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/common/report"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/driver"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/filesetup"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/primitives"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)
// SubAnalyzer drives the mutex / rwmutex misuse checks as an independent
// analysis.Analyzer. It does not call pass.Report itself: it returns the
// prepared diagnostic slice as Result so the umbrella Analyzer can
// re-emit them (and so analysistest can observe them through the
// umbrella).
var SubAnalyzer = &analysis.Analyzer{
Name: "goconcurrencylint_mutex",
Doc: "Detects misuse of sync.Mutex and sync.RWMutex.",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer, primitives.Analyzer, filesetup.Analyzer},
ResultType: reflect.TypeFor[[]analysis.Diagnostic](),
}
func run(pass *analysis.Pass) (any, error) {
// scope is built once on first use and shared across every function in the
// pass, so the package-wide indexes and lifecycle caches are not rebuilt per
// function. driver.Run visits functions sequentially, so the lazy init needs
// no synchronization.
var scope *packageScope
return driver.Run(pass, driver.Config[*Checker]{
Guard: primitives.HasMutexes,
NewChecker: func(fr *primitives.FunctionResult, ec report.Reporter, cf *commentfilter.CommentFilter, pass *analysis.Pass) *Checker {
if scope == nil {
scope = newPackageScope(pass.Files)
}
return NewChecker(fr, ec, cf, pass.TypesInfo, scope)
},
})
}