Skip to content

pkg/golinters: add noreplace #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions assets/github-action-config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"MinorVersionToConfig": {
"latest": {
"TargetVersion": "v1.37.1",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.37.1/golangci-lint-1.37.1-linux-amd64.tar.gz"
"TargetVersion": "v1.38.0",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.38.0/golangci-lint-1.38.0-linux-amd64.tar.gz"
},
"v1.10": {
"Error": "golangci-lint version 'v1.10' isn't supported: we support only v1.14.0 and later versions"
Expand Down Expand Up @@ -115,6 +115,10 @@
"TargetVersion": "v1.37.1",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.37.1/golangci-lint-1.37.1-linux-amd64.tar.gz"
},
"v1.38": {
"TargetVersion": "v1.38.0",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.38.0/golangci-lint-1.38.0-linux-amd64.tar.gz"
},
"v1.4": {
"Error": "golangci-lint version 'v1.4' isn't supported: we support only v1.14.0 and later versions"
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a
git.sr.ht/~urandom/noreplace v0.0.0-20210303085701-97f39e5a1bba
github.com/BurntSushi/toml v0.3.1
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
github.com/OpenPeeDeeP/depguard v1.0.1
Expand Down
5 changes: 4 additions & 1 deletion go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions pkg/golinters/noreplace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package golinters

import (
"sync"

"git.sr.ht/~urandom/noreplace"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const noreplaceName = "noreplace"

// NewNoreplace returns a new noreplace linter.
func NewNoreplace() *goanalysis.Linter {
var issues []goanalysis.Issue
var mu sync.Mutex
analyzer := &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
noreplaceName,
"Block the use of replace directives Go modules.",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
pp, err := noreplace.Check()
if err != nil {
lintCtx.Log.Warnf("running %s failed: %s: if you are not using go modules "+
"it is suggested to disable this linter", noreplaceName, err)
return nil, nil
}
mu.Lock()
defer mu.Unlock()
for _, p := range pp {
issues = append(issues, goanalysis.NewIssue(&result.Issue{
FromLinter: noreplaceName,
Pos: p[0],
LineRange: &result.Range{From: p[0].Line, To: p[1].Line},
Replacement: &result.Replacement{NeedOnlyDelete: true},
Text: noreplace.Text,
}, pass))
}
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return issues
})
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),
linter.NewConfig(golinters.NewNoreplace()).
WithAutoFix().
WithURL("https://git.sr.ht/~urandom/noreplace"),

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