Skip to content

Commit

Permalink
Make API less pointery. Get rid of error interface.
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Chlebek <eric@sensu.io>
  • Loading branch information
echlebek committed Dec 10, 2020
1 parent 6afc191 commit 97c82e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
14 changes: 5 additions & 9 deletions errcheck/errcheck.go
Expand Up @@ -114,15 +114,15 @@ func (b byName) Len() int {
}

// Append appends errors to e. Append does not do any duplicate checking.
func (r *Result) Append(other *Result) {
func (r *Result) Append(other Result) {
r.UncheckedErrors = append(r.UncheckedErrors, other.UncheckedErrors...)
}

// Returns the unique errors that have been accumulated. Duplicates may occur
// when a file containing an unchecked error belongs to > 1 package.
//
// The method receiver remains unmodified after the call to Unique.
func (r *Result) Unique() *Result {
func (r Result) Unique() Result {
result := make([]UncheckedError, len(r.UncheckedErrors))
copy(result, r.UncheckedErrors)
sort.Sort((byName)(result))
Expand All @@ -132,11 +132,7 @@ func (r *Result) Unique() *Result {
uniq = append(uniq, err)
}
}
return &Result{UncheckedErrors: uniq}
}

func (r *Result) Error() string {
return fmt.Sprintf("%d unchecked errors", len(r.UncheckedErrors))
return Result{UncheckedErrors: uniq}
}

// Exclusions define symbols and language elements that will be not checked
Expand Down Expand Up @@ -232,7 +228,7 @@ func (c *Checker) shouldSkipFile(file *ast.File) bool {
//
// It will exclude specific errors from analysis if the user has configured
// exclusions.
func (c *Checker) CheckPackage(pkg *packages.Package) *Result {
func (c *Checker) CheckPackage(pkg *packages.Package) Result {
excludedSymbols := map[string]bool{}
for _, sym := range c.Exclusions.Symbols {
excludedSymbols[sym] = true
Expand Down Expand Up @@ -268,7 +264,7 @@ func (c *Checker) CheckPackage(pkg *packages.Package) *Result {
}
ast.Walk(v, astFile)
}
return &Result{UncheckedErrors: v.errors}
return Result{UncheckedErrors: v.errors}
}

// visitor implements the errcheck algorithm
Expand Down
8 changes: 4 additions & 4 deletions errcheck/errcheck_test.go
Expand Up @@ -188,7 +188,7 @@ package custom
for _, pkg := range packages {
uerr.Append(checker.CheckPackage(pkg))
}
uerr = uerr.Unique()
*uerr = uerr.Unique()
if test.numExpectedErrs == 0 {
if len(uerr.UncheckedErrors) != 0 {
t.Errorf("expected no errors, but got: %v", uerr)
Expand Down Expand Up @@ -297,7 +297,7 @@ require github.com/testlog v0.0.0
for _, pkg := range packages {
uerr.Append(checker.CheckPackage(pkg))
}
uerr = uerr.Unique()
*uerr = uerr.Unique()

if test.numExpectedErrs == 0 {
if len(uerr.UncheckedErrors) != 0 {
Expand Down Expand Up @@ -393,7 +393,7 @@ require github.com/testlog v0.0.0
if err != nil {
t.Fatal(err)
}
uerr := &Result{}
uerr := Result{}
for _, pkg := range packages {
uerr.Append(checker.CheckPackage(pkg))
}
Expand Down Expand Up @@ -429,7 +429,7 @@ func test(t *testing.T, f flags) {
if err != nil {
t.Fatal(err)
}
uerr := &Result{}
uerr := Result{}
numErrors := len(uncheckedMarkers)
if blank {
numErrors += len(blankMarkers)
Expand Down
8 changes: 4 additions & 4 deletions main.go
Expand Up @@ -87,7 +87,7 @@ func (f *tagsFlag) Set(s string) error {
return nil
}

func reportResult(e *errcheck.Result) {
func reportResult(e errcheck.Result) {
wd, err := os.Getwd()
if err != nil {
wd = ""
Expand Down Expand Up @@ -138,16 +138,16 @@ func mainCmd(args []string) int {
return exitCodeOk
}

func checkPaths(c *errcheck.Checker, paths ...string) (*errcheck.Result, error) {
func checkPaths(c *errcheck.Checker, paths ...string) (errcheck.Result, error) {
pkgs, err := c.LoadPackages(paths...)
if err != nil {
return nil, err
return errcheck.Result{}, err
}
// Check for errors in the initial packages.
work := make(chan *packages.Package, len(pkgs))
for _, pkg := range pkgs {
if len(pkg.Errors) > 0 {
return nil, fmt.Errorf("errors while loading package %s: %v", pkg.ID, pkg.Errors)
return errcheck.Result{}, fmt.Errorf("errors while loading package %s: %v", pkg.ID, pkg.Errors)
}
work <- pkg
}
Expand Down

0 comments on commit 97c82e3

Please sign in to comment.