-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SA1032: add check for errors.Is and errors.As argument order
Closes: gh-1530 Closes: gh-1545 [via git-merge-pr] Co-authored-by: Dominik Honnef <[email protected]>
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package sa1032 | ||
|
||
import ( | ||
"honnef.co/go/tools/analysis/callcheck" | ||
"honnef.co/go/tools/analysis/lint" | ||
"honnef.co/go/tools/go/ir" | ||
"honnef.co/go/tools/internal/passes/buildir" | ||
"honnef.co/go/tools/knowledge" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ | ||
Analyzer: &analysis.Analyzer{ | ||
Name: "SA1032", | ||
Requires: []*analysis.Analyzer{buildir.Analyzer}, | ||
Run: callcheck.Analyzer(rules), | ||
}, | ||
Doc: &lint.RawDocumentation{ | ||
Title: `Wrong order of arguments to \'errors.Is\' or \'errors.As\'`, | ||
Text: ` | ||
The first argument of the functions \'errors.As\' and \'errors.Is\' is the error | ||
that we have and the second argument is the error we're trying to match against. | ||
For example: | ||
if errors.Is(err, io.EOF) { ... } | ||
This check detects some cases where the two arguments have been swapped. It | ||
flags any calls where the first argument is referring to a package-level error | ||
variable, such as | ||
if errors.Is(io.EOF, err) { /* this is wrong */ }`, | ||
Since: "Unreleased", | ||
Severity: lint.SeverityError, | ||
MergeIf: lint.MergeIfAny, | ||
}, | ||
}) | ||
|
||
var Analyzer = SCAnalyzer.Analyzer | ||
|
||
var rules = map[string]callcheck.Check{ | ||
"errors.As": func(call *callcheck.Call) { | ||
validateIs(call.Pass.Pkg.Path(), call.Args[knowledge.Arg("errors.As.err")]) | ||
}, | ||
"errors.Is": func(call *callcheck.Call) { | ||
validateIs(call.Pass.Pkg.Path(), call.Args[knowledge.Arg("errors.Is.err")]) | ||
}, | ||
} | ||
|
||
func validateIs(curPkg string, arg *callcheck.Argument) { | ||
v, ok := arg.Value.Value.(*ir.Load) | ||
if !ok { | ||
return | ||
} | ||
g, ok := v.X.(*ir.Global) | ||
if !ok { | ||
return | ||
} | ||
if pkg := g.Package().Pkg; pkg != nil && pkg.Path() != curPkg { | ||
arg.Invalid("arguments have the wrong order") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
staticcheck/sa1032/testdata/go1.0/example.com/ErrorsOrder/ErrorsOrder.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
) | ||
|
||
var gErr = errors.New("global") | ||
|
||
type myErr struct{} | ||
|
||
func (myErr) Error() string { return "" } | ||
|
||
func is() { | ||
err := errors.New("oh noes") | ||
|
||
_ = errors.Is(err, fs.ErrNotExist) | ||
_ = errors.Is(fs.ErrNotExist, err) //@ diag(`wrong order`) | ||
if errors.Is(err, fs.ErrNotExist) { | ||
} | ||
if errors.Is(fs.ErrNotExist, err) { //@ diag(`wrong order`) | ||
} | ||
|
||
_ = errors.Is(gErr, fs.ErrNotExist) | ||
_ = errors.Is(fs.ErrNotExist, gErr) //@ diag(`wrong order`) | ||
if errors.Is(gErr, fs.ErrNotExist) { | ||
} | ||
if errors.Is(fs.ErrNotExist, gErr) { //@ diag(`wrong order`) | ||
} | ||
|
||
_ = errors.Is(myErr{}, fs.ErrNotExist) | ||
_ = errors.Is(fs.ErrNotExist, myErr{}) //@ diag(`wrong order`) | ||
if errors.Is(myErr{}, fs.ErrNotExist) { | ||
} | ||
if errors.Is(fs.ErrNotExist, myErr{}) { //@ diag(`wrong order`) | ||
} | ||
|
||
_ = errors.Is(&myErr{}, fs.ErrNotExist) | ||
_ = errors.Is(fs.ErrNotExist, &myErr{}) //@ diag(`wrong order`) | ||
if errors.Is(&myErr{}, fs.ErrNotExist) { | ||
} | ||
if errors.Is(fs.ErrNotExist, &myErr{}) { //@ diag(`wrong order`) | ||
} | ||
} | ||
|
||
func as() { | ||
err := errors.New("oh noes") | ||
|
||
_ = errors.As(err, fs.ErrNotExist) | ||
_ = errors.As(fs.ErrNotExist, err) //@ diag(`wrong order`) | ||
if errors.As(err, fs.ErrNotExist) { | ||
} | ||
if errors.As(fs.ErrNotExist, err) { //@ diag(`wrong order`) | ||
} | ||
|
||
_ = errors.As(gErr, fs.ErrNotExist) | ||
_ = errors.As(fs.ErrNotExist, gErr) //@ diag(`wrong order`) | ||
if errors.As(gErr, fs.ErrNotExist) { | ||
} | ||
if errors.As(fs.ErrNotExist, gErr) { //@ diag(`wrong order`) | ||
} | ||
|
||
_ = errors.As(myErr{}, fs.ErrNotExist) | ||
_ = errors.As(fs.ErrNotExist, myErr{}) //@ diag(`wrong order`) | ||
if errors.As(myErr{}, fs.ErrNotExist) { | ||
} | ||
if errors.As(fs.ErrNotExist, myErr{}) { //@ diag(`wrong order`) | ||
} | ||
|
||
_ = errors.As(&myErr{}, fs.ErrNotExist) | ||
_ = errors.As(fs.ErrNotExist, &myErr{}) //@ diag(`wrong order`) | ||
if errors.As(&myErr{}, fs.ErrNotExist) { | ||
} | ||
if errors.As(fs.ErrNotExist, &myErr{}) { //@ diag(`wrong order`) | ||
} | ||
} |