-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defer FP on return statement in a function literal passed to the deferred function #863
Comments
Hi @feldgendler thanks for filling the issue. |
@feldgendler, I've created a PR with a fix to avoid the false positive. Please check if the fix works for you (and if is an acceptable approach) |
Yes, thank you! It fixes our case. It's definitely a step in the right direction (towards being less aggressive). I see what kind of bug you are trying to prevent, though, so here is a suggestion. I would completely replace this particular rule (“return in a defer function has no effect”) with an entirely different one: case *ast.DeferStmt:
if fl, ok := n.Call.Fun.(*ast.FuncLit); ok && fl.Type.Results != nil {
w.newFailure("deferred function with useless return values", 1, "logic", "return")
} That's it. You don't need to look inside for a And if |
While we're at it: while reading I'm not sure I understand correctly the exact logic behind the This simple rule will flag all of these, which definitely look like bugs: func main() {
e := recover() // not in a deferred function
...
}
...
defer recover() // doesn't do what one might naively guess
defer f(recover()) // doesn't do what one might naively guess
defer func() {
e := func() any {
return recover() // Go reference manual says this won't work
}()
...
}()
defer f(func() any {
return recover() // won't work even if f calls its argument
}) The only kind of false positive that I can think of is func f() {
e := recover()
...
}
...
defer f() This might be correct code, but I'd say it's bad style, and anyone who thinks otherwise should disable the Final note: you might want to extend the rule from just |
To pull in the fix for mgechev/revive#863 - seen in the GitHub Actions check below: https://github.com/tailscale/tailscale/actions/runs/11057524933/job/30721507353?pr=13600 Updates #13602 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: Ia04adc5d74bdbde14204645ca948794447b16776
To pull in the fix for mgechev/revive#863 - seen in the GitHub Actions check below: https://github.com/tailscale/tailscale/actions/runs/11057524933/job/30721507353?pr=13600 Updates #13602 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: Ia04adc5d74bdbde14204645ca948794447b16776
To pull in the fix for mgechev/revive#863 - seen in the GitHub Actions check below: https://github.com/tailscale/tailscale/actions/runs/11057524933/job/30721507353?pr=13600 Updates #13602 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: Ia04adc5d74bdbde14204645ca948794447b16776
To pull in the fix for mgechev/revive#863 - seen in the GitHub Actions check below: https://github.com/tailscale/tailscale/actions/runs/11057524933/job/30721507353?pr=13600 Updates #13602 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: Ia04adc5d74bdbde14204645ca948794447b16776
Describe the bug
The
defer
check misfires on areturn
statement inside a function literal passed to the deferred function.To Reproduce
Steps to reproduce the behavior:
go install github.com/mgechev/revive@latest
revive.toml:
test.go:
Here,
verify
executes the given function and verifies that it has succeeded. We use this pattern in our project, and I don't see anything wrong with it.Although
return
has no effect in a deferred function, this one is not in the deferrede function; it's in a function that's passed as an argument to the deferred function.Expected behavior
No
defer
issue is reported.Logs
Desktop (please complete the following information):
go version go1.20.6 linux/amd64
The text was updated successfully, but these errors were encountered: