From a594777528d69da7978e1e90b205d6cdd7dda29f Mon Sep 17 00:00:00 2001 From: Ryan Boehning Date: Sat, 24 Mar 2018 11:26:29 -0700 Subject: [PATCH] Update golint Change import path from github.com/golang/lint to golang.org/x/lint. --- .travis.yml | 2 +- Gopkg.lock | 26 ++-- Gopkg.toml | 5 +- .../golang => golang.org/x}/lint/LICENSE | 0 .../x}/lint/golint/golint.go | 2 +- .../x}/lint/golint/import.go | 7 +- .../golang => golang.org/x}/lint/lint.go | 118 +++++++----------- 7 files changed, 67 insertions(+), 93 deletions(-) rename vendor/{github.com/golang => golang.org/x}/lint/LICENSE (100%) rename vendor/{github.com/golang => golang.org/x}/lint/golint/golint.go (99%) rename vendor/{github.com/golang => golang.org/x}/lint/golint/import.go (98%) rename vendor/{github.com/golang => golang.org/x}/lint/lint.go (95%) diff --git a/.travis.yml b/.travis.yml index d58cf57..ac12d42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ notifications: # install code checkers before_script: - - cd vendor/github.com/golang/lint/golint + - cd vendor/golang.org/x/lint/golint - go install - cd - - cd vendor/honnef.co/go/tools/cmd/megacheck diff --git a/Gopkg.lock b/Gopkg.lock index 90b0b4b..9f4817e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -8,22 +8,13 @@ revision = "6acd4345c835499920e8426c7e4e8d7a34f1bb83" [[projects]] - branch = "master" - name = "github.com/golang/lint" - packages = [ - ".", - "golint" - ] - revision = "e14d9b0f1d332b1420c1ffa32562ad2dc84d645d" - -[[projects]] - branch = "master" name = "github.com/kisielk/gotool" packages = [ ".", "internal/load" ] - revision = "d6ce6262d87e3a4e153e86023ff56ae771554a41" + revision = "80517062f582ea3340cd4baf70e86d539ae7d84d" + version = "v1.0.0" [[projects]] branch = "master" @@ -37,6 +28,15 @@ packages = ["."] revision = "7cafcd837844e784b526369c9bce262804aebc60" +[[projects]] + branch = "master" + name = "golang.org/x/lint" + packages = [ + ".", + "golint" + ] + revision = "85993ffd0a6cd043291f3f63d45d656d97b165bd" + [[projects]] branch = "master" name = "golang.org/x/tools" @@ -48,7 +48,7 @@ "go/loader", "go/types/typeutil" ] - revision = "25101aadb97aa42907eee6a238d6d26a6cb3c756" + revision = "4c0f0e48a635ba08a80fbebdb19258a0f675a910" [[projects]] name = "honnef.co/go/tools" @@ -75,6 +75,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "8affb7b5cc460e34b10774c525cf8bfcbb13290a47cf7f7a5e251df422fb540e" + inputs-digest = "f9817690650739b7e8cc7c487924fbe7043fc818348569fdb9050e440c080287" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 7530a24..bb8fc20 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -1,6 +1,7 @@ required = [ "github.com/fzipp/gocyclo", - "github.com/golang/lint/golint", + "golang.org/x/lint", + "golang.org/x/lint/golint", "honnef.co/go/tools/cmd/megacheck", ] @@ -9,7 +10,7 @@ name = "github.com/fzipp/gocyclo" branch = "master" [[constraint]] -name = "github.com/golang/lint" +name = "golang.org/x/lint" branch = "master" [prune] diff --git a/vendor/github.com/golang/lint/LICENSE b/vendor/golang.org/x/lint/LICENSE similarity index 100% rename from vendor/github.com/golang/lint/LICENSE rename to vendor/golang.org/x/lint/LICENSE diff --git a/vendor/github.com/golang/lint/golint/golint.go b/vendor/golang.org/x/lint/golint/golint.go similarity index 99% rename from vendor/github.com/golang/lint/golint/golint.go rename to vendor/golang.org/x/lint/golint/golint.go index d8360ad..ac024b6 100644 --- a/vendor/github.com/golang/lint/golint/golint.go +++ b/vendor/golang.org/x/lint/golint/golint.go @@ -16,7 +16,7 @@ import ( "path/filepath" "strings" - "github.com/golang/lint" + "golang.org/x/lint" ) var ( diff --git a/vendor/github.com/golang/lint/golint/import.go b/vendor/golang.org/x/lint/golint/import.go similarity index 98% rename from vendor/github.com/golang/lint/golint/import.go rename to vendor/golang.org/x/lint/golint/import.go index 02a0daa..2ba9dea 100644 --- a/vendor/github.com/golang/lint/golint/import.go +++ b/vendor/golang.org/x/lint/golint/import.go @@ -22,11 +22,10 @@ import ( "strings" ) -var buildContext = build.Default - var ( - goroot = filepath.Clean(runtime.GOROOT()) - gorootSrc = filepath.Join(goroot, "src") + buildContext = build.Default + goroot = filepath.Clean(runtime.GOROOT()) + gorootSrc = filepath.Join(goroot, "src") ) // importPathsNoDotExpansion returns the import paths to use for the given diff --git a/vendor/github.com/golang/lint/lint.go b/vendor/golang.org/x/lint/lint.go similarity index 95% rename from vendor/github.com/golang/lint/lint.go rename to vendor/golang.org/x/lint/lint.go index 8bb1faa..46bd45f 100644 --- a/vendor/github.com/golang/lint/lint.go +++ b/vendor/golang.org/x/lint/lint.go @@ -23,6 +23,7 @@ import ( "unicode" "unicode/utf8" + "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/gcexportdata" ) @@ -199,7 +200,6 @@ func (f *file) lint() { f.lintNames() f.lintVarDecls() f.lintElses() - f.lintIfError() f.lintRanges() f.lintErrorf() f.lintErrors() @@ -527,7 +527,10 @@ func (f *file) lintExported() { }) } -var allCapsRE = regexp.MustCompile(`^[A-Z0-9_]+$`) +var ( + allCapsRE = regexp.MustCompile(`^[A-Z0-9_]+$`) + anyCapsRE = regexp.MustCompile(`[A-Z]`) +) // knownNameExceptions is a set of names that are known to be exempt from naming checks. // This is usually because they are constrained by having to match names in the @@ -544,6 +547,9 @@ func (f *file) lintNames() { if strings.Contains(f.f.Name.Name, "_") && !strings.HasSuffix(f.f.Name.Name, "_test") { f.errorf(f.f, 1, link("http://golang.org/doc/effective_go.html#package-names"), category("naming"), "don't use an underscore in package name") } + if anyCapsRE.MatchString(f.f.Name.Name) { + f.errorf(f.f, 1, link("http://golang.org/doc/effective_go.html#package-names"), category("mixed-caps"), "don't use MixedCaps in package name; %s should be %s", f.f.Name.Name, strings.ToLower(f.f.Name.Name)) + } check := func(id *ast.Ident, thing string) { if id.Name == "_" { @@ -1039,13 +1045,13 @@ func (f *file) lintElses() { if !ok || ifStmt.Else == nil { return true } - if ignore[ifStmt] { - return true - } if elseif, ok := ifStmt.Else.(*ast.IfStmt); ok { ignore[elseif] = true return true } + if ignore[ifStmt] { + return true + } if _, ok := ifStmt.Else.(*ast.BlockStmt); !ok { // only care about elses without conditions return true @@ -1078,20 +1084,25 @@ func (f *file) lintRanges() { if !ok { return true } - if rs.Value == nil { - // for x = range m { ... } - return true // single var form - } - if !isIdent(rs.Value, "_") { - // for ?, y = range m { ... } + + if isIdent(rs.Key, "_") && (rs.Value == nil || isIdent(rs.Value, "_")) { + p := f.errorf(rs.Key, 1, category("range-loop"), "should omit values from range; this loop is equivalent to `for range ...`") + + newRS := *rs // shallow copy + newRS.Value = nil + newRS.Key = nil + p.ReplacementLine = f.firstLineOf(&newRS, rs) + return true } - p := f.errorf(rs.Value, 1, category("range-loop"), "should omit 2nd value from range; this loop is equivalent to `for %s %s range ...`", f.render(rs.Key), rs.Tok) + if isIdent(rs.Value, "_") { + p := f.errorf(rs.Value, 1, category("range-loop"), "should omit 2nd value from range; this loop is equivalent to `for %s %s range ...`", f.render(rs.Key), rs.Tok) - newRS := *rs // shallow copy - newRS.Value = nil - p.ReplacementLine = f.firstLineOf(&newRS, rs) + newRS := *rs // shallow copy + newRS.Value = nil + p.ReplacementLine = f.firstLineOf(&newRS, rs) + } return true }) @@ -1115,6 +1126,9 @@ func (f *file) lintErrorf() { if !isErrorsNew && !isTestingError { return true } + if !f.imports("errors") { + return true + } arg := ce.Args[0] ce, ok = arg.(*ast.CallExpr) if !ok || !isPkgDot(ce.Fun, "fmt", "Sprintf") { @@ -1295,6 +1309,9 @@ func (f *file) lintErrorReturn() { if len(ret) <= 1 { return true } + if isIdent(ret[len(ret)-1].Type, "error") { + return true + } // An error return parameter should be the last parameter. // Flag any error parameters found before the last. for _, r := range ret[:len(ret)-1] { @@ -1489,63 +1506,6 @@ func (f *file) containsComments(start, end token.Pos) bool { return false } -func (f *file) lintIfError() { - f.walk(func(node ast.Node) bool { - switch v := node.(type) { - case *ast.BlockStmt: - for i := 0; i < len(v.List)-1; i++ { - // if var := whatever; var != nil { return var } - s, ok := v.List[i].(*ast.IfStmt) - if !ok || s.Body == nil || len(s.Body.List) != 1 || s.Else != nil { - continue - } - assign, ok := s.Init.(*ast.AssignStmt) - if !ok || len(assign.Lhs) != 1 || !(assign.Tok == token.DEFINE || assign.Tok == token.ASSIGN) { - continue - } - id, ok := assign.Lhs[0].(*ast.Ident) - if !ok { - continue - } - expr, ok := s.Cond.(*ast.BinaryExpr) - if !ok || expr.Op != token.NEQ { - continue - } - if lhs, ok := expr.X.(*ast.Ident); !ok || lhs.Name != id.Name { - continue - } - if rhs, ok := expr.Y.(*ast.Ident); !ok || rhs.Name != "nil" { - continue - } - r, ok := s.Body.List[0].(*ast.ReturnStmt) - if !ok || len(r.Results) != 1 { - continue - } - if r, ok := r.Results[0].(*ast.Ident); !ok || r.Name != id.Name { - continue - } - - // return nil - r, ok = v.List[i+1].(*ast.ReturnStmt) - if !ok || len(r.Results) != 1 { - continue - } - if r, ok := r.Results[0].(*ast.Ident); !ok || r.Name != "nil" { - continue - } - - // check if there are any comments explaining the construct, don't emit an error if there are some. - if f.containsComments(s.Pos(), r.Pos()) { - continue - } - - f.errorf(v.List[i], 0.9, "redundant if ...; err != nil check, just return error instead.") - } - } - return true - }) -} - // receiverType returns the named type of the method receiver, sans "*", // or "invalid-type" if fn.Recv is ill formed. func receiverType(fn *ast.FuncDecl) string { @@ -1683,6 +1643,20 @@ func (f *file) srcLineWithMatch(node ast.Node, pattern string) (m []string) { return rx.FindStringSubmatch(line) } +// imports returns true if the current file imports the specified package path. +func (f *file) imports(importPath string) bool { + all := astutil.Imports(f.fset, f.f) + for _, p := range all { + for _, i := range p { + uq, err := strconv.Unquote(i.Path.Value) + if err == nil && importPath == uq { + return true + } + } + } + return false +} + // srcLine returns the complete line at p, including the terminating newline. func srcLine(src []byte, p token.Position) string { // Run to end of line in both directions if not at line start/end.