Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.5k
Add gomodclean linter#5898
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -764,6 +764,7 @@ | ||
| "godox", | ||
| "err113", | ||
| "goheader", | ||
| "gomodclean", | ||
| "gomoddirectives", | ||
| "gomodguard", | ||
| "goprintffuncname", | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package gomodclean | ||
| import ( | ||
| "sync" | ||
| gomodcleananalyzer "github.com/dmrioja/gomodclean/pkg/analyzer" | ||
| "golang.org/x/tools/go/analysis" | ||
| "github.com/golangci/golangci-lint/v2/pkg/goanalysis" | ||
| "github.com/golangci/golangci-lint/v2/pkg/lint/linter" | ||
| "github.com/golangci/golangci-lint/v2/pkg/result" | ||
| ) | ||
| const linterName = "gomodclean" | ||
| func New() *goanalysis.Linter { | ||
| var issues []*goanalysis.Issue | ||
| var once sync.Once | ||
| analyzer := &analysis.Analyzer{ | ||
| Name: linterName, | ||
| Doc: "Linter to check dependencies are well structured inside your go.mod file.", | ||
| Run: goanalysis.DummyRun, | ||
| } | ||
| return goanalysis. | ||
| NewLinterFromAnalyzer(analyzer). | ||
| WithContextSetter(func(lintCtx *linter.Context) { | ||
| analyzer.Run = func(pass *analysis.Pass) (any, error) { | ||
| once.Do(func() { | ||
| results, err := gomodcleananalyzer.Analyze() | ||
| if err != nil { | ||
| lintCtx.Log.Warnf("running %s failed: %s: "+ | ||
| "if you are not using go modules it is suggested to disable this linter", linterName, err) | ||
| return | ||
| } | ||
| for _, p := range results { | ||
| issues = append(issues, goanalysis.NewIssue(&result.Issue{ | ||
| FromLinter: linterName, | ||
| Pos: p.Position, | ||
| Text: p.Text, | ||
| }, pass)) | ||
| } | ||
| }) | ||
| return nil, nil | ||
| } | ||
| }). | ||
| WithIssuesReporter(func(*linter.Context) []*goanalysis.Issue { | ||
| return issues | ||
| }). | ||
| WithLoadMode(goanalysis.LoadModeSyntax) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package gomodclean | ||
| import ( | ||
| "testing" | ||
| "github.com/golangci/golangci-lint/v2/test/testshared/integration" | ||
| ) | ||
| func TestFromTestdata(t *testing.T) { | ||
| integration.RunTestdata(t) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module gomodclean | ||
| go 1.24.2 | ||
| require github.com/dmrioja/gomodclean v0.1.1-0.20250628101813-52ee7046c81a | ||
| require golang.org/x/mod v0.25.0 // indirect |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //golangcitest:args -Egomodclean | ||
| package testdata | ||
| import ( | ||
| "log" | ||
| "github.com/dmrioja/gomodclean/pkg/analyzer" | ||
| ) | ||
| // correctGoModFile imports some dependencies to build the tesdata go.mod file. | ||
| func correctGoModFile() { //nolint:unused | ||
| results, err := analyzer.Analyze() | ||
| log.Println(results) | ||
| log.Println(err) | ||
| } |