-
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
Incorrectly identifies channel draining as "empty code block" #386
Comments
Looks like we should type check to verify that we're dealing with a channel. This would slow down the rule though, but seems like the right way to go. |
For the sake of rule simplicity, I think these corner cases should be handled by disabling the linter in the concerned lines. |
@jpopadak, because of some regressions (both stability and performance), we reverted the fix. It's still possible to recognize this pattern without type checking. This is something we'd investigate in the future. |
Would be nice to have this fixed. I've a project with dozen of such false positives and have to disable the rule. :( |
branch https://github.com/mgechev/revive/tree/fix-386 implements a fix consisting in trying to identify channel draining loops by their syntax (a range without value nor key) and avoids raising a failure for such cases branch https://github.com/mgechev/revive/tree/alleviates-386 also identifies channel draining loops by their syntax but in place of not raising a failure it raises one with a lower (0.5) confidence. Neither implementation uses type information |
The fix for this bug was reverted soon after being introduced. Are the reasons in #416 still valid? That was in 2020, and some things might have changed since (for instance, Go modules are now the norm). I can see that many other revive checks use type information. Can't this one? |
Hi @feldgendler, thanks for commenting in this thread. |
Hi! Thanks for coming back to me @chavacava! Do I understand correctly that if any revive rule that needs type information is enabled, that triggers type analysis, and once it's triggered, any such rules have negligible marginal cost? Also, is it true that all revive rules that currently require type information are disabled by default? |
Yes, and yes |
Would it be possible to add a fine-tuning option to the |
I've rebased the branch #415 and now the rule does not warn on empty blocks on channel draining ranges. |
@chavacava Yes, thank you! My code doesn't drigger |
For info, I believe I've hit this same issue with the following code: https://github.com/twpayne/go-heap/blob/fb16bd2df012f1689aa9c0682b0c611078c10908/heap.go#L39-L40 revive incorrectly reports that the block can be removed:
This is with revive run by golangci-lint 1.61.0, i.e. revive v1.3.9. |
Hi @twpayne thanks for reporting the issue. |
Thank you very much for the analysis and explanation. I've indeed added a I agree that any complete static analysis is infeasible. Maybe a couple of heuristics like "does any part of the for clause, condition, or range clause contain a function call or an assignment to a variable outside the loop" might help reduce the false positives of this rule. Just a thought, I don't know how easy this would be to implement or how well it work in practice. |
Or |
@twpayne the rule already have a very primitive version of such an heuristic but as you mention (and @feldgendler comment confirms) the cases are "infinite". |
Ack, makes sense. Is it worth closing this issue as "not planned" or "won't fix"? |
If I had a say, I would err for the side of false negatives rather than false positives. In this context, it means that an empty loop body is not reported as “empty code block” — either never reported at all, or only reported if the loop statement itself is definitely side-effect-free (that is, if in doubt, don't report). |
Describe the bug
revive
incorrectly identities channel draining as an empty code block and suggests to remove it.To Reproduce
Steps to reproduce the behavior:
go get -u github.com/mgechev/revive
A really bad example, but in our scenario, we have many routines reading from
processChan
where we send another message to have them stop processing. We reuse the channel, but we want to ensure the channel was empty from previous processing.Expected behavior
No error to occur as this is channel draining.
Logs
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Additional context
We have two workarounds:
revive
knew that this is channel draining.The text was updated successfully, but these errors were encountered: