1
0
mirror of https://github.com/mgechev/revive.git synced 2025-03-25 21:29:16 +02:00

fix error return rule to allow multiple error return values (#110)

* fix error return rule to allow multiple error return values

please check golint related updates here: golang/lint@8379672

When returning multiple values with an error,
lint checks if the error is the last return value.
But the implementation actually is checking for all return values
except for the last one, and throw the alert if it found an error.

There is a (edge) case where some function returning more than one error
is getting a false positive even when the last return value is an error.

This patch adds an early check, to see if the last return value is an error
and if so, it will pass silently.

* Fix return error

* add test changes
This commit is contained in:
Dian 2019-03-21 02:54:27 +08:00 committed by Minko Gechev
parent fbefad8558
commit 44eed7edb7
2 changed files with 13 additions and 0 deletions

View File

@ -41,3 +41,13 @@ func l() (int, error, int) { // MATCH /error should be the last type when return
func m() (x int, err error, y int) { // MATCH /error should be the last type when returning multiple items/
return 0, nil, 0
}
// Check for multiple error returns but with errors at the end.
func n() (int, error, error) { // ok
return 0, nil, nil
}
// Check for multiple error returns mixed in order, but keeping one error at last position.
func o() (int, error, int, error) { // ok
return 0, nil, 0, nil
}

View File

@ -47,6 +47,9 @@ func (w lintErrorReturn) Visit(n ast.Node) ast.Visitor {
if len(ret) <= 1 {
return w
}
if isIdent(ret[len(ret)-1].Type, "error") {
return nil
}
// An error return parameter should be the last parameter.
// Flag any error parameters found before the last.
for _, r := range ret[:len(ret)-1] {