From b2f2446b46bf02821d90ebedadae2bf7ae0e880e Mon Sep 17 00:00:00 2001 From: Taras Madan Date: Mon, 5 Sep 2022 14:27:54 +0200 Subject: go.mod, vendor: update (#3358) * go.mod, vendor: remove unnecessary dependencies Commands: 1. go mod tidy 2. go mod vendor * go.mod, vendor: update cloud.google.com/go Commands: 1. go get -u cloud.google.com/go 2. go mod tidy 3. go mod vendor * go.mod, vendor: update cloud.google.com/* Commands: 1. go get -u cloud.google.com/storage cloud.google.com/logging 2. go mod tidy 3. go mod vendor * go.mod, .golangci.yml, vendor: update *lint* Commands: 1. go get -u golang.org/x/tools github.com/golangci/golangci-lint@v1.47.0 2. go mod tidy 3. go mod vendor 4. edit .golangci.yml to suppress new errors (resolved in the same PR later) * all: fix lint errors hash.go: copy() recommended by gosimple parse.go: ent is never nil verifier.go: signal.Notify() with unbuffered channel is bad. Have no idea why. * .golangci.yml: adjust godot rules check-all is deprecated, but still work if you're hesitating too - I'll remove this commit --- .../ashanbrown/forbidigo/forbidigo/forbidigo.go | 34 ++++++++------ .../ashanbrown/forbidigo/forbidigo/patterns.go | 43 ++++++++++++++++++ .../ashanbrown/makezero/makezero/makezero.go | 53 ++++++++++++++++++---- 3 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 vendor/github.com/ashanbrown/forbidigo/forbidigo/patterns.go (limited to 'vendor/github.com/ashanbrown') diff --git a/vendor/github.com/ashanbrown/forbidigo/forbidigo/forbidigo.go b/vendor/github.com/ashanbrown/forbidigo/forbidigo/forbidigo.go index 794a43ad2..17740faa7 100644 --- a/vendor/github.com/ashanbrown/forbidigo/forbidigo/forbidigo.go +++ b/vendor/github.com/ashanbrown/forbidigo/forbidigo/forbidigo.go @@ -24,10 +24,15 @@ type UsedIssue struct { identifier string pattern string position token.Position + customMsg string } func (a UsedIssue) Details() string { - return fmt.Sprintf("use of `%s` forbidden by pattern `%s`", a.identifier, a.pattern) + explanation := fmt.Sprintf(` because %q`, a.customMsg) + if a.customMsg == "" { + explanation = fmt.Sprintf(" by pattern `%s`", a.pattern) + } + return fmt.Sprintf("use of `%s` forbidden", a.identifier) + explanation } func (a UsedIssue) Position() token.Position { @@ -36,17 +41,17 @@ func (a UsedIssue) Position() token.Position { func (a UsedIssue) String() string { return toString(a) } -func toString(i Issue) string { +func toString(i UsedIssue) string { return fmt.Sprintf("%s at %s", i.Details(), i.Position()) } type Linter struct { cfg config - patterns []*regexp.Regexp + patterns []*pattern } func DefaultPatterns() []string { - return []string{`^fmt\.Print(|f|ln)$`} + return []string{`^(fmt\.Print(|f|ln)|print|println)$`} } //go:generate go-options config @@ -65,13 +70,13 @@ func NewLinter(patterns []string, options ...Option) (*Linter, error) { if len(patterns) == 0 { patterns = DefaultPatterns() } - compiledPatterns := make([]*regexp.Regexp, 0, len(patterns)) - for _, p := range patterns { - re, err := regexp.Compile(p) + compiledPatterns := make([]*pattern, 0, len(patterns)) + for _, ptrn := range patterns { + p, err := parse(ptrn) if err != nil { - return nil, fmt.Errorf("unable to compile pattern `%s`: %s", p, err) + return nil, err } - compiledPatterns = append(compiledPatterns, re) + compiledPatterns = append(compiledPatterns, p) } return &Linter{ cfg: cfg, @@ -91,7 +96,7 @@ type visitor struct { } func (l *Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) { - var issues []Issue // nolint:prealloc // we don't know how many there will be + var issues []Issue //nolint:prealloc // we don't know how many there will be for _, node := range nodes { var comments []*ast.CommentGroup isTestFile := false @@ -158,11 +163,12 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor { return v } for _, p := range v.linter.patterns { - if p.MatchString(v.textFor(node)) && !v.permit(node) { + if p.pattern.MatchString(v.textFor(node)) && !v.permit(node) { v.issues = append(v.issues, UsedIssue{ identifier: v.textFor(node), - pattern: p.String(), + pattern: p.pattern.String(), position: v.fset.Position(node.Pos()), + customMsg: p.msg, }) } } @@ -182,10 +188,10 @@ func (v *visitor) permit(node ast.Node) bool { return false } nodePos := v.fset.Position(node.Pos()) - var nolint = regexp.MustCompile(fmt.Sprintf(`^permit:%s\b`, regexp.QuoteMeta(v.textFor(node)))) + var nolint = regexp.MustCompile(fmt.Sprintf(`^//\s?permit:%s\b`, regexp.QuoteMeta(v.textFor(node)))) for _, c := range v.comments { commentPos := v.fset.Position(c.Pos()) - if commentPos.Line == nodePos.Line && nolint.MatchString(c.Text()) { + if commentPos.Line == nodePos.Line && len(c.List) > 0 && nolint.MatchString(c.List[0].Text) { return true } } diff --git a/vendor/github.com/ashanbrown/forbidigo/forbidigo/patterns.go b/vendor/github.com/ashanbrown/forbidigo/forbidigo/patterns.go new file mode 100644 index 000000000..c23648822 --- /dev/null +++ b/vendor/github.com/ashanbrown/forbidigo/forbidigo/patterns.go @@ -0,0 +1,43 @@ +package forbidigo + +import ( + "fmt" + "regexp" + "regexp/syntax" + "strings" +) + +type pattern struct { + pattern *regexp.Regexp + msg string +} + +func parse(ptrn string) (*pattern, error) { + ptrnRe, err := regexp.Compile(ptrn) + if err != nil { + return nil, fmt.Errorf("unable to compile pattern `%s`: %s", ptrn, err) + } + re, err := syntax.Parse(ptrn, syntax.Perl) + if err != nil { + return nil, fmt.Errorf("unable to parse pattern `%s`: %s", ptrn, err) + } + msg := extractComment(re) + return &pattern{pattern: ptrnRe, msg: msg}, nil +} + +// Traverse the leaf submatches in the regex tree and extract a comment, if any +// is present. +func extractComment(re *syntax.Regexp) string { + for _, sub := range re.Sub { + if len(sub.Sub) > 0 { + if comment := extractComment(sub); comment != "" { + return comment + } + } + subStr := sub.String() + if strings.HasPrefix(subStr, "#") { + return strings.TrimSpace(strings.TrimPrefix(subStr, "#")) + } + } + return "" +} diff --git a/vendor/github.com/ashanbrown/makezero/makezero/makezero.go b/vendor/github.com/ashanbrown/makezero/makezero/makezero.go index 6bf230b12..18bcad3d0 100644 --- a/vendor/github.com/ashanbrown/makezero/makezero/makezero.go +++ b/vendor/github.com/ashanbrown/makezero/makezero/makezero.go @@ -1,4 +1,4 @@ -// makezero provides a linter for appends to slices initialized with non-zero length. +// Package makezero provides a linter for appends to slices initialized with non-zero length. package makezero import ( @@ -12,14 +12,23 @@ import ( "regexp" ) +// a decl might include multiple var, +// so var name with decl make final uniq obj. +type uniqDecl struct { + varName string + decl interface{} +} + type Issue interface { Details() string + Pos() token.Pos Position() token.Position String() string } type AppendIssue struct { name string + pos token.Pos position token.Position } @@ -27,6 +36,10 @@ func (a AppendIssue) Details() string { return fmt.Sprintf("append to slice `%s` with non-zero initialized length", a.name) } +func (a AppendIssue) Pos() token.Pos { + return a.pos +} + func (a AppendIssue) Position() token.Position { return a.position } @@ -35,6 +48,7 @@ func (a AppendIssue) String() string { return toString(a) } type MustHaveNonZeroInitLenIssue struct { name string + pos token.Pos position token.Position } @@ -42,6 +56,10 @@ func (i MustHaveNonZeroInitLenIssue) Details() string { return fmt.Sprintf("slice `%s` does not have non-zero initial length", i.name) } +func (i MustHaveNonZeroInitLenIssue) Pos() token.Pos { + return i.pos +} + func (i MustHaveNonZeroInitLenIssue) Position() token.Position { return i.position } @@ -58,7 +76,7 @@ type visitor struct { comments []*ast.CommentGroup // comments to apply during this visit info *types.Info - nonZeroLengthSliceDecls map[interface{}]struct{} + nonZeroLengthSliceDecls map[uniqDecl]struct{} fset *token.FileSet issues []Issue } @@ -74,14 +92,14 @@ func NewLinter(initialLengthMustBeZero bool) *Linter { } func (l Linter) Run(fset *token.FileSet, info *types.Info, nodes ...ast.Node) ([]Issue, error) { - var issues []Issue // nolint:prealloc // don't know how many there will be + var issues []Issue for _, node := range nodes { var comments []*ast.CommentGroup if file, ok := node.(*ast.File); ok { comments = file.Comments } visitor := visitor{ - nonZeroLengthSliceDecls: make(map[interface{}]struct{}), + nonZeroLengthSliceDecls: make(map[uniqDecl]struct{}), initLenMustBeZero: l.initLenMustBeZero, info: info, fset: fset, @@ -103,7 +121,12 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor { if sliceIdent, ok := node.Args[0].(*ast.Ident); ok && v.hasNonZeroInitialLength(sliceIdent) && !v.hasNoLintOnSameLine(fun) { - v.issues = append(v.issues, AppendIssue{name: sliceIdent.Name, position: v.fset.Position(fun.Pos())}) + v.issues = append(v.issues, + AppendIssue{ + name: sliceIdent.Name, + pos: fun.Pos(), + position: v.fset.Position(fun.Pos()), + }) } case *ast.AssignStmt: for i, right := range node.Rhs { @@ -116,13 +139,14 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor { if len(right.Args) == 2 { // ignore if not a slice or it has explicit zero length if !v.isSlice(right.Args[0]) { - break + continue } else if lit, ok := right.Args[1].(*ast.BasicLit); ok && lit.Kind == token.INT && lit.Value == "0" { - break + continue } if v.initLenMustBeZero && !v.hasNoLintOnSameLine(fun) { v.issues = append(v.issues, MustHaveNonZeroInitLenIssue{ name: v.textFor(left), + pos: node.Pos(), position: v.fset.Position(node.Pos()), }) } @@ -148,7 +172,10 @@ func (v *visitor) hasNonZeroInitialLength(ident *ast.Ident) bool { ident.Name, v.fset.Position(ident.Pos()).String()) return false } - _, exists := v.nonZeroLengthSliceDecls[ident.Obj.Decl] + _, exists := v.nonZeroLengthSliceDecls[uniqDecl{ + varName: ident.Obj.Name, + decl: ident.Obj.Decl, + }] return exists } @@ -157,7 +184,13 @@ func (v *visitor) recordNonZeroLengthSlices(node ast.Node) { if !ok { return } - v.nonZeroLengthSliceDecls[ident.Obj.Decl] = struct{}{} + if ident.Obj == nil { + return + } + v.nonZeroLengthSliceDecls[uniqDecl{ + varName: ident.Obj.Name, + decl: ident.Obj.Decl, + }] = struct{}{} } func (v *visitor) isSlice(node ast.Node) bool { @@ -185,7 +218,7 @@ func (v *visitor) isSlice(node ast.Node) bool { } func (v *visitor) hasNoLintOnSameLine(node ast.Node) bool { - var nolint = regexp.MustCompile(`^\s*nozero\b`) + nolint := regexp.MustCompile(`^\s*nozero\b`) nodePos := v.fset.Position(node.Pos()) for _, c := range v.comments { commentPos := v.fset.Position(c.Pos()) -- cgit mrf-deployment