Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Commit

Permalink
lint: avoid false positives with custom errors-package
Browse files Browse the repository at this point in the history
Fixes #350
  • Loading branch information
leonelquinteros committed Mar 1, 2018
1 parent 6aaf7c3 commit cac6d06
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"unicode"
"unicode/utf8"

"golang.org/x/tools/go/ast/astutil"

"golang.org/x/tools/go/gcexportdata"
)

Expand Down Expand Up @@ -1115,6 +1117,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") {
Expand Down Expand Up @@ -1688,6 +1693,21 @@ 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.
Expand Down

0 comments on commit cac6d06

Please sign in to comment.