-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyzer.go
More file actions
88 lines (85 loc) · 3.48 KB
/
Copy pathanalyzer.go
File metadata and controls
88 lines (85 loc) · 3.48 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
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
// Package analyzer is the public entry point for the goconcurrencylint
// linter. It exposes a single *analysis.Analyzer that the singlechecker
// binary and golangci-lint can consume.
//
// Internally the work is split across sub-analyzers wired together via
// the go/analysis Requires graph. The umbrella Analyzer is the only
// exported one; the rest live under internal/ and are composed through
// pass.ResultOf.
//
// Analyzer (umbrella, this package)
// │ re-emits the diagnostic slices below via pass.Report,
// │ prefixing each message with its check code (e.g. "GCL1001: ...")
// │
// ├── mutex.SubAnalyzer ──────┐
// ├── waitgroup.SubAnalyzer ──┤── requires primitives.Analyzer ─┐
// ├── once.SubAnalyzer ───────┤── requires filesetup.Analyzer │
// ├── cond.SubAnalyzer ───────┤ │
// ├── pool.SubAnalyzer ───────┤ │
// ├── channel.SubAnalyzer ────┤ │
// └── copycheck.Analyzer ─────┘ └── requires inspect.Analyzer
//
// "A requires B" means B runs first and exposes its Result through
// pass.ResultOf[B]. Each sub-analyzer returns its diagnostic slice as a
// Result instead of calling pass.Report directly. The umbrella below
// re-emits them on its own pass so analysistest and any other consumer
// that targets the umbrella sees the complete diagnostic set.
package analyzer
import (
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/channel"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/common/category"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/cond"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/copycheck"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/mutex"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/once"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/pool"
"github.com/sanbricio/goconcurrencylint/pkg/analyzer/internal/waitgroup"
"golang.org/x/tools/go/analysis"
)
var Analyzer = &analysis.Analyzer{
Name: "goconcurrencylint",
Doc: "Detects misuse of sync.Mutex, sync.RWMutex, sync.WaitGroup, sync.Once, sync.Cond, sync.Pool and channels, plus copy-by-value of sync primitives.",
Run: run,
Requires: []*analysis.Analyzer{
mutex.SubAnalyzer,
waitgroup.SubAnalyzer,
once.SubAnalyzer,
cond.SubAnalyzer,
pool.SubAnalyzer,
channel.SubAnalyzer,
copycheck.Analyzer,
},
}
func run(pass *analysis.Pass) (any, error) {
subs := []*analysis.Analyzer{
mutex.SubAnalyzer,
waitgroup.SubAnalyzer,
once.SubAnalyzer,
cond.SubAnalyzer,
pool.SubAnalyzer,
channel.SubAnalyzer,
copycheck.Analyzer,
}
for _, sub := range subs {
diags, ok := pass.ResultOf[sub].([]analysis.Diagnostic)
if !ok {
continue
}
for _, d := range diags {
// Applied here, the single point every diagnostic converges on,
// so the sub-analyzers stay unaware of it.
if !selection.enabled(category.Category(d.Category)) {
continue
}
// Surface the check code in the message itself (e.g.
// "GCL1001: ...") so it is visible in plain CLI output, which
// otherwise prints only file:line:col + message. The Category
// field already carries the same code for tooling.
if d.Category != "" {
d.Message = d.Category + ": " + d.Message
}
pass.Report(d)
}
}
return nil, nil
}