From 97c82e37fad1fbb99917469123753ce5ae8a07c3 Mon Sep 17 00:00:00 2001 From: Eric Chlebek Date: Wed, 9 Dec 2020 22:21:58 -0800 Subject: [PATCH] Make API less pointery. Get rid of error interface. Signed-off-by: Eric Chlebek --- errcheck/errcheck.go | 14 +++++--------- errcheck/errcheck_test.go | 8 ++++---- main.go | 8 ++++---- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/errcheck/errcheck.go b/errcheck/errcheck.go index 010c849..74bfaf0 100644 --- a/errcheck/errcheck.go +++ b/errcheck/errcheck.go @@ -114,7 +114,7 @@ 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...) } @@ -122,7 +122,7 @@ func (r *Result) Append(other *Result) { // 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)) @@ -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 @@ -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 @@ -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 diff --git a/errcheck/errcheck_test.go b/errcheck/errcheck_test.go index bc9422d..472c023 100644 --- a/errcheck/errcheck_test.go +++ b/errcheck/errcheck_test.go @@ -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) @@ -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 { @@ -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)) } @@ -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) diff --git a/main.go b/main.go index f29b183..6369927 100644 --- a/main.go +++ b/main.go @@ -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 = "" @@ -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 }