Skip to content

Commit

Permalink
Merge pull request #197 from SVilgelm/selector-name
Browse files Browse the repository at this point in the history
Add SelectorName in UncheckedError
  • Loading branch information
kisielk committed Feb 25, 2021
2 parents ee08a45 + aeac866 commit 4174a4a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
42 changes: 38 additions & 4 deletions errcheck/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ var (

// UncheckedError indicates the position of an unchecked error return.
type UncheckedError struct {
Pos token.Position
Line string
FuncName string
Pos token.Position
Line string
FuncName string
SelectorName string
}

// Result is returned from the CheckPackage function, and holds all the errors
Expand Down Expand Up @@ -335,6 +336,37 @@ func (v *visitor) fullName(call *ast.CallExpr) string {
return fn.FullName()
}

func getSelectorName(sel *ast.SelectorExpr) string {
if ident, ok := sel.X.(*ast.Ident); ok {
return fmt.Sprintf("%s.%s", ident.Name, sel.Sel.Name)
}
if s, ok := sel.X.(*ast.SelectorExpr); ok {
return fmt.Sprintf("%s.%s", getSelectorName(s), sel.Sel.Name)
}

return ""
}

// selectorName will return a name for a called function
// if the function is the result of a selector. Otherwise it will return
// the empty string.
//
// The name is fully qualified by the import path, possible type,
// function/method name and pointer receiver.
//
// For example,
// - for "fmt.Printf(...)" it will return "fmt.Printf"
// - for "base64.StdEncoding.Decode(...)" it will return "base64.StdEncoding.Decode"
// - for "myFunc()" it will return ""
func (v *visitor) selectorName(call *ast.CallExpr) string {
sel, _, ok := v.selectorAndFunc(call)
if !ok {
return ""
}

return getSelectorName(sel)
}

// namesForExcludeCheck will return a list of fully-qualified function names
// from a function call that can be used to check against the exclusion list.
//
Expand Down Expand Up @@ -531,11 +563,13 @@ func (v *visitor) addErrorAtPosition(position token.Pos, call *ast.CallExpr) {
}

var name string
var sel string
if call != nil {
name = v.fullName(call)
sel = v.selectorName(call)
}

v.errors = append(v.errors, UncheckedError{pos, line, name})
v.errors = append(v.errors, UncheckedError{pos, line, name, sel})
}

func readfile(filename string) []string {
Expand Down
3 changes: 3 additions & 0 deletions errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,5 +502,8 @@ func test(t *testing.T, f flags) {
if !uncheckedMarkers[m] && !blankMarkers[m] && !assertMarkers[m] {
t.Errorf("%d: unexpected error: %v", i, err)
}
if err.SelectorName != "" && !strings.Contains(err.Line, err.SelectorName) {
t.Errorf("the line '%s' must contain the selector '%s'", err.Line, err.SelectorName)
}
}
}

0 comments on commit 4174a4a

Please sign in to comment.