Open florianl opened 2 months ago
Note that the provided example doesn't compile, because that's not shadowing but trying to redeclare. And if it had worked, it would've been an unused variable and also hadn't compiled.
Do you have a closer-to-reality example you'd like to see flagged by Staticcheck?
This is the example from the Go specification (last example in the Return statements
section):
func f(n int) (res int, err error) {
if _, err := f(n-1); err != nil {
return // invalid return statement: err is shadowed
}
return
}
Better to flag all naked returns IMHO. That's more opinionated, but as far as I know there's fairly wide agreement that they should pretty much always be avoided. That's #170.
I'm using revive to detect "naked return", aka "bare return"
https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#bare-return
staticcheck
should detect the shadowing of named returns.In the following example, the function
foo()
uses the named returnerr error
. In this case, the named return is shadowed byerr := bar()
.