Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ linters:
- godot
- godox
- goheader
- gomodclean
- gomoddirectives
- gomodguard
- goprintffuncname
Expand Down Expand Up @@ -172,6 +173,7 @@ linters:
- godot
- godox
- goheader
- gomodclean
- gomoddirectives
- gomodguard
- goprintffuncname
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/curioswitch/go-reassign v0.3.0
github.com/daixiang0/gci v0.13.6
github.com/denis-tingaikin/go-header v0.5.0
github.com/dmrioja/gomodclean v0.2.0
github.com/fatih/color v1.18.0
github.com/firefart/nonamedreturns v1.0.6
github.com/fzipp/gocyclo v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

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

1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@
"godox",
"err113",
"goheader",
"gomodclean",
"gomoddirectives",
"gomodguard",
"goprintffuncname",
Expand Down
55 changes: 55 additions & 0 deletions pkg/golinters/gomodclean/gomodclean.go
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)
}
11 changes: 11 additions & 0 deletions pkg/golinters/gomodclean/gomodclean_integration_test.go
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)
}
7 changes: 7 additions & 0 deletions pkg/golinters/gomodclean/testdata/go.mod
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
12 changes: 12 additions & 0 deletions pkg/golinters/gomodclean/testdata/go.sum

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

15 changes: 15 additions & 0 deletions pkg/golinters/gomodclean/testdata/gomodclean.go
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)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/golangci/golangci-lint/v2/pkg/golinters/goheader"
"github.com/golangci/golangci-lint/v2/pkg/golinters/goimports"
"github.com/golangci/golangci-lint/v2/pkg/golinters/golines"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gomodclean"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gomoddirectives"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gomodguard"
"github.com/golangci/golangci-lint/v2/pkg/golinters/goprintffuncname"
Expand Down Expand Up @@ -375,6 +376,10 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithSince("v1.22.0").
WithURL("https://github.com/tommy-muehle/go-mnd"),

linter.NewConfig(gomodclean.New()).
WithSince("v2.2.0").
WithURL("https://github.com/dmrioja/gomodclean"),

linter.NewConfig(gomoddirectives.New(&cfg.Linters.Settings.GoModDirectives)).
WithSince("v1.39.0").
WithURL("https://github.com/ldez/gomoddirectives"),
Expand Down