Skip to content

Commit 91c44b0

Browse files
committed
pkg/golinters: add noreplace
Add support for enforcing that go.mod should not contain replace clauses.
1 parent d4ee818 commit 91c44b0

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

assets/github-action-config.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"MinorVersionToConfig": {
33
"latest": {
4-
"TargetVersion": "v1.37.1",
5-
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.37.1/golangci-lint-1.37.1-linux-amd64.tar.gz"
4+
"TargetVersion": "v1.38.0",
5+
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.38.0/golangci-lint-1.38.0-linux-amd64.tar.gz"
66
},
77
"v1.10": {
88
"Error": "golangci-lint version 'v1.10' isn't supported: we support only v1.14.0 and later versions"
@@ -115,6 +115,10 @@
115115
"TargetVersion": "v1.37.1",
116116
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.37.1/golangci-lint-1.37.1-linux-amd64.tar.gz"
117117
},
118+
"v1.38": {
119+
"TargetVersion": "v1.38.0",
120+
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.38.0/golangci-lint-1.38.0-linux-amd64.tar.gz"
121+
},
118122
"v1.4": {
119123
"Error": "golangci-lint version 'v1.4' isn't supported: we support only v1.14.0 and later versions"
120124
},

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a
7+
git.sr.ht/~urandom/noreplace v0.0.0-20210303085701-97f39e5a1bba
78
github.com/BurntSushi/toml v0.3.1
89
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
910
github.com/OpenPeeDeeP/depguard v1.0.1

go.sum

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/noreplace.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package golinters
2+
3+
import (
4+
"sync"
5+
6+
"git.sr.ht/~urandom/noreplace"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
10+
"github.com/golangci/golangci-lint/pkg/lint/linter"
11+
"github.com/golangci/golangci-lint/pkg/result"
12+
)
13+
14+
const noreplaceName = "noreplace"
15+
16+
// NewNoreplace returns a new noreplace linter.
17+
func NewNoreplace() *goanalysis.Linter {
18+
var issues []goanalysis.Issue
19+
var mu sync.Mutex
20+
analyzer := &analysis.Analyzer{
21+
Name: goanalysis.TheOnlyAnalyzerName,
22+
Doc: goanalysis.TheOnlyanalyzerDoc,
23+
}
24+
return goanalysis.NewLinter(
25+
noreplaceName,
26+
"Block the use of replace directives Go modules.",
27+
[]*analysis.Analyzer{analyzer},
28+
nil,
29+
).WithContextSetter(func(lintCtx *linter.Context) {
30+
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+
}
48+
return nil, nil
49+
}
50+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
51+
return issues
52+
})
53+
}

pkg/lint/lintersdb/manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +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()).
398+
WithAutoFix().
399+
WithURL("https://git.sr.ht/~urandom/noreplace"),
397400

398401
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
399402
linter.NewConfig(golinters.NewNoLintLint()).

0 commit comments

Comments
 (0)