Skip to content
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

bump golang.org/x/tools to HEAD #2875

Merged
merged 3 commits into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions go.mod
Expand Up @@ -99,7 +99,7 @@ require (
github.com/yagipy/maintidx v1.0.0
github.com/yeya24/promlinter v0.2.0
gitlab.com/bosi/decorder v0.2.1
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a
golang.org/x/tools v0.1.11-0.20220518213611-904e24e9fcf9
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
honnef.co/go/tools v0.3.1
mvdan.cc/gofumpt v0.3.1
Expand Down Expand Up @@ -165,11 +165,10 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
9 changes: 4 additions & 5 deletions go.sum

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

14 changes: 8 additions & 6 deletions pkg/golinters/depguard.go
Expand Up @@ -15,23 +15,25 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

const depguardLinterName = "depguard"
const depguardName = "depguard"

func NewDepguard() *goanalysis.Linter {
func NewDepguard(settings *config.DepGuardSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: depguardLinterName,
Name: depguardName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: goanalysis.DummyRun,
}

return goanalysis.NewLinter(
depguardLinterName,
depguardName,
"Go linter that checks if package imports are in a list of acceptable packages",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
dg, err := newDepGuard(&lintCtx.Settings().Depguard)
dg, err := newDepGuard(settings)

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
if err != nil {
Expand Down Expand Up @@ -153,7 +155,7 @@ func (g guardian) run(loadConfig *loader.Config, prog *loader.Program, pass *ana
goanalysis.NewIssue(&result.Issue{
Pos: issue.Position,
Text: g.createMsg(issue.PackageName),
FromLinter: depguardLinterName,
FromLinter: depguardName,
}, pass),
)
}
Expand Down
64 changes: 39 additions & 25 deletions pkg/golinters/dogsled.go
Expand Up @@ -8,51 +8,65 @@ import (

"golang.org/x/tools/go/analysis"

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

const dogsledLinterName = "dogsled"
const dogsledName = "dogsled"

func NewDogsled() *goanalysis.Linter {
//nolint:dupl
func NewDogsled(settings *config.DogsledSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: dogsledLinterName,
Name: dogsledName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
dogsledLinterName,
"Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var pkgIssues []goanalysis.Issue
for _, f := range pass.Files {
v := returnsVisitor{
maxBlanks: lintCtx.Settings().Dogsled.MaxBlankIdentifiers,
f: pass.Fset,
}
ast.Walk(&v, f)
for i := range v.issues {
pkgIssues = append(pkgIssues, goanalysis.NewIssue(&v.issues[i], pass))
}
Run: func(pass *analysis.Pass) (interface{}, error) {
issues := runDogsled(pass, settings)

if len(issues) == 0 {
return nil, nil
}

mu.Lock()
resIssues = append(resIssues, pkgIssues...)
resIssues = append(resIssues, issues...)
mu.Unlock()

return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
},
}

return goanalysis.NewLinter(
dogsledName,
"Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())",
[]*analysis.Analyzer{analyzer},
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func runDogsled(pass *analysis.Pass, settings *config.DogsledSettings) []goanalysis.Issue {
var reports []goanalysis.Issue
for _, f := range pass.Files {
v := &returnsVisitor{
maxBlanks: settings.MaxBlankIdentifiers,
f: pass.Fset,
}

ast.Walk(v, f)

for i := range v.issues {
reports = append(reports, goanalysis.NewIssue(&v.issues[i], pass))
}
}

return reports
}

type returnsVisitor struct {
f *token.FileSet
maxBlanks int
Expand Down Expand Up @@ -87,7 +101,7 @@ func (v *returnsVisitor) Visit(node ast.Node) ast.Visitor {

if numBlank > v.maxBlanks {
v.issues = append(v.issues, result.Issue{
FromLinter: dogsledLinterName,
FromLinter: dogsledName,
Text: fmt.Sprintf("declaration has %v blank identifiers", numBlank),
Pos: v.f.Position(assgnStmt.Pos()),
})
Expand Down
108 changes: 63 additions & 45 deletions pkg/golinters/dupl.go
Expand Up @@ -9,36 +9,25 @@ import (
"github.com/pkg/errors"
"golang.org/x/tools/go/analysis"

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

const duplLinterName = "dupl"
const duplName = "dupl"

func NewDupl() *goanalysis.Linter {
//nolint:dupl
func NewDupl(settings *config.DuplSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: duplLinterName,
Name: duplName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
duplLinterName,
"Tool for code clone detection",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

issues, err := duplAPI.Run(fileNames, lintCtx.Settings().Dupl.Threshold)
Run: func(pass *analysis.Pass) (interface{}, error) {
issues, err := runDupl(pass, settings)
if err != nil {
return nil, err
}
Expand All @@ -47,37 +36,66 @@ func NewDupl() *goanalysis.Linter {
return nil, nil
}

res := make([]goanalysis.Issue, 0, len(issues))
for _, i := range issues {
toFilename, err := fsutils.ShortestRelPath(i.To.Filename(), "")
if err != nil {
return nil, errors.Wrapf(err, "failed to get shortest rel path for %q", i.To.Filename())
}
dupl := fmt.Sprintf("%s:%d-%d", toFilename, i.To.LineStart(), i.To.LineEnd())
text := fmt.Sprintf("%d-%d lines are duplicate of %s",
i.From.LineStart(), i.From.LineEnd(),
formatCode(dupl, lintCtx.Cfg))
res = append(res, goanalysis.NewIssue(&result.Issue{
Pos: token.Position{
Filename: i.From.Filename(),
Line: i.From.LineStart(),
},
LineRange: &result.Range{
From: i.From.LineStart(),
To: i.From.LineEnd(),
},
Text: text,
FromLinter: duplLinterName,
}, pass))
}

mu.Lock()
resIssues = append(resIssues, res...)
resIssues = append(resIssues, issues...)
mu.Unlock()

return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
},
}

return goanalysis.NewLinter(
duplName,
"Tool for code clone detection",
[]*analysis.Analyzer{analyzer},
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

issues, err := duplAPI.Run(fileNames, settings.Threshold)
if err != nil {
return nil, err
}

if len(issues) == 0 {
return nil, nil
}

res := make([]goanalysis.Issue, 0, len(issues))

for _, i := range issues {
toFilename, err := fsutils.ShortestRelPath(i.To.Filename(), "")
if err != nil {
return nil, errors.Wrapf(err, "failed to get shortest rel path for %q", i.To.Filename())
}

dupl := fmt.Sprintf("%s:%d-%d", toFilename, i.To.LineStart(), i.To.LineEnd())
text := fmt.Sprintf("%d-%d lines are duplicate of %s",
i.From.LineStart(), i.From.LineEnd(),
formatCode(dupl, nil))

res = append(res, goanalysis.NewIssue(&result.Issue{
Pos: token.Position{
Filename: i.From.Filename(),
Line: i.From.LineStart(),
},
LineRange: &result.Range{
From: i.From.LineStart(),
To: i.From.LineEnd(),
},
Text: text,
FromLinter: duplName,
}, pass))
}

return res, nil
}