1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-10 03:17:11 +02:00
revive/fixtures/golint/error-return.go
Dian 44eed7edb7 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
2019-03-20 11:54:27 -07:00

54 lines
1.3 KiB
Go

// Test for returning errors.
// Package foo ...
package foo
// Returns nothing
func f() { // ok
}
// Check for a single error return
func g() error { // ok
return nil
}
// Check for a single other return type
func h() int { // ok
return 0
}
// Check for multiple return but error at end.
func i() (int, error) { // ok
return 0, nil
}
// Check for multiple return but error at end with named variables.
func j() (x int, err error) { // ok
return 0, nil
}
// Check for error in the wrong location on 2 types
func k() (error, int) { // MATCH /error should be the last type when returning multiple items/
return nil, 0
}
// Check for error in the wrong location for > 2 types
func l() (int, error, int) { // MATCH /error should be the last type when returning multiple items/
return 0, nil, 0
}
// Check for error in the wrong location with named variables.
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
}