@@ -4,50 +4,53 @@ import (
4
4
"sync"
5
5
6
6
"git.sr.ht/~urandom/noreplace"
7
- "golang.org/x/tools/go/analysis"
8
-
9
7
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
10
8
"github.com/golangci/golangci-lint/pkg/lint/linter"
11
9
"github.com/golangci/golangci-lint/pkg/result"
10
+ "golang.org/x/tools/go/analysis"
12
11
)
13
12
14
- const noreplaceName = "noreplace"
13
+ const noReplaceName = "noreplace"
15
14
16
- // NewNoreplace returns a new noreplace linter.
17
- func NewNoreplace () * goanalysis.Linter {
15
+ // NewNoReplace returns a new noreplace linter.
16
+ func NewNoReplace () * goanalysis.Linter {
18
17
var issues []goanalysis.Issue
19
- var mu sync.Mutex
18
+ var once sync.Once
19
+
20
20
analyzer := & analysis.Analyzer {
21
21
Name : goanalysis .TheOnlyAnalyzerName ,
22
22
Doc : goanalysis .TheOnlyanalyzerDoc ,
23
23
}
24
+
24
25
return goanalysis .NewLinter (
25
- noreplaceName ,
26
+ noReplaceName ,
26
27
"Block the use of replace directives Go modules." ,
27
28
[]* analysis.Analyzer {analyzer },
28
29
nil ,
29
30
).WithContextSetter (func (lintCtx * linter.Context ) {
30
31
analyzer .Run = func (pass * analysis.Pass ) (interface {}, error ) {
31
- pp , err := noreplace .Check ()
32
- if err != nil {
33
- lintCtx .Log .Warnf ("running %s failed: %s: if you are not using go modules " +
34
- "it is suggested to disable this linter" , noreplaceName , err )
35
- return nil , nil
36
- }
37
- mu .Lock ()
38
- defer mu .Unlock ()
39
- for _ , p := range pp {
40
- issues = append (issues , goanalysis .NewIssue (& result.Issue {
41
- FromLinter : noreplaceName ,
42
- Pos : p [0 ],
43
- LineRange : & result.Range {From : p [0 ].Line , To : p [1 ].Line },
44
- Replacement : & result.Replacement {NeedOnlyDelete : true },
45
- Text : noreplace .Text ,
46
- }, pass ))
47
- }
32
+ once .Do (func () {
33
+ pp , err := noreplace .Check ()
34
+ if err != nil {
35
+ lintCtx .Log .Warnf ("running %s failed: %s: if you are not using go modules " +
36
+ "it is suggested to disable this linter" , noReplaceName , err )
37
+ return
38
+ }
39
+
40
+ for _ , p := range pp {
41
+ issues = append (issues , goanalysis .NewIssue (& result.Issue {
42
+ FromLinter : noReplaceName ,
43
+ Pos : p [0 ],
44
+ LineRange : & result.Range {From : p [0 ].Line , To : p [1 ].Line },
45
+ Replacement : & result.Replacement {NeedOnlyDelete : true },
46
+ Text : noreplace .Text ,
47
+ }, pass ))
48
+ }
49
+ })
50
+
48
51
return nil , nil
49
52
}
50
53
}).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
51
54
return issues
52
- })
55
+ }). WithLoadMode ( goanalysis . LoadModeSyntax )
53
56
}
0 commit comments