Skip to content

Commit 11f852d

Browse files
committed
fix: noreplace.
1 parent 91c44b0 commit 11f852d

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

pkg/golinters/noreplace.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,53 @@ import (
44
"sync"
55

66
"git.sr.ht/~urandom/noreplace"
7-
"golang.org/x/tools/go/analysis"
8-
97
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
108
"github.com/golangci/golangci-lint/pkg/lint/linter"
119
"github.com/golangci/golangci-lint/pkg/result"
10+
"golang.org/x/tools/go/analysis"
1211
)
1312

14-
const noreplaceName = "noreplace"
13+
const noReplaceName = "noreplace"
1514

16-
// NewNoreplace returns a new noreplace linter.
17-
func NewNoreplace() *goanalysis.Linter {
15+
// NewNoReplace returns a new noreplace linter.
16+
func NewNoReplace() *goanalysis.Linter {
1817
var issues []goanalysis.Issue
19-
var mu sync.Mutex
18+
var once sync.Once
19+
2020
analyzer := &analysis.Analyzer{
2121
Name: goanalysis.TheOnlyAnalyzerName,
2222
Doc: goanalysis.TheOnlyanalyzerDoc,
2323
}
24+
2425
return goanalysis.NewLinter(
25-
noreplaceName,
26+
noReplaceName,
2627
"Block the use of replace directives Go modules.",
2728
[]*analysis.Analyzer{analyzer},
2829
nil,
2930
).WithContextSetter(func(lintCtx *linter.Context) {
3031
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+
4851
return nil, nil
4952
}
5053
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
5154
return issues
52-
})
55+
}).WithLoadMode(goanalysis.LoadModeSyntax)
5356
}

pkg/lint/lintersdb/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
394394
WithPresets(linter.PresetStyle).
395395
WithLoadForGoAnalysis().
396396
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),
397-
linter.NewConfig(golinters.NewNoreplace()).
397+
linter.NewConfig(golinters.NewNoReplace()).
398+
WithPresets(linter.PresetStyle).
399+
WithLoadForGoAnalysis().
398400
WithAutoFix().
399401
WithURL("https://git.sr.ht/~urandom/noreplace"),
400402

0 commit comments

Comments
 (0)