-
-
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 .As() argument order
Fixes #1530
- Loading branch information
Showing
5 changed files
with
148 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,55 @@ | ||
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.Documentation{ | ||
Title: `Swapped arguments in errors.Is() or errors.As()`, | ||
Text: ` | ||
If the first argument to \'errors.As()\' or \'errors.Is()\' contains a package | ||
selector it assumes the error and target were swapped, since the target should | ||
practically always be a local variable and not a reference to another package. | ||
`, | ||
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`) | ||
} | ||
} |