revive: Incorrectly identifies channel draining as "empty code block"
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:
- I updated revive
go get -u github.com/mgechev/revive - I run it with the following flags & configuration file:
cd $PROJECT_DIR
revive -config revive.toml .
ignoreGeneratedHeader = false
severity = "error"
confidence = 0.8
errorCode = 1
warningCode = 0
[rule.add-constant]
arguments = [{maxLitCount = "3",allowStrs ="\"\"",allowInts="0,1,2"}]
[rule.argument-limit]
arguments = [6]
[rule.atomic]
[rule.blank-imports]
[rule.bool-literal-in-expr]
[rule.call-to-gc]
[rule.constant-logical-expr]
[rule.context-as-argument]
[rule.context-keys-type]
[rule.cyclomatic]
arguments = [10]
[rule.deep-exit]
[rule.dot-imports]
[rule.duplicated-imports]
[rule.empty-block]
[rule.error-naming]
[rule.error-return]
[rule.error-strings]
[rule.errorf]
[rule.exported]
[rule.flag-parameter]
[rule.function-result-limit]
arguments = [4]
[rule.get-return]
[rule.if-return]
[rule.import-shadowing]
[rule.increment-decrement]
[rule.indent-error-flow]
[rule.line-length-limit]
arguments = [120]
[rule.package-comments]
[rule.range]
[rule.range-val-in-closure]
[rule.receiver-naming]
[rule.redefines-builtin-id]
[rule.superfluous-else]
[rule.time-naming]
[rule.unexported-return]
[rule.unhandled-error]
[rule.unreachable-code]
[rule.unused-parameter]
[rule.unused-receiver]
[rule.var-declaration]
[rule.var-naming]
[rule.waitgroup-by-value]
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.
package main
import "sync"
const (
chanSize = 10
maxNums = 100
modNum = 5
)
func main() {
wg := sync.WaitGroup{}
processChan := make(chan int, chanSize)
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < maxNums; i++ {
processChan <- i
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < maxNums; i++ {
if !isValidNum(i) {
break
}
}
}()
for range processChan {
}
}
func isValidNum(i int) bool {
return i%modNum == 0
}
- Revive returns an error
myflie.go:34:24: this block is empty, you can remove it
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):
- OS: macOS 10.14.6
- Version of Go: v1.14.1
Additional context We have two workarounds:
- We can ignore the few lines across our project where we are iterating over a channel, however, it would be nicer if
reviveknew that this is channel draining. - We can delete and recreate the channel, but since the channel is passed around on startup / Dependency Injection, we cannot just “create a new one and reinitialize all services” easily.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 4
- Comments: 20 (2 by maintainers)
Commits related to this issue
- fix #386 by structurally identifying channel draining ranges — committed to mgechev/revive by chavacava 5 years ago
- fix #386 by structurally identifying channel draining ranges — committed to mgechev/revive by chavacava 5 years ago
Would be nice to have this fixed. I’ve a project with dozen of such false positives and have to disable the rule. 😦
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).
I’ve rebased the branch #415 and now the rule does not warn on empty blocks on channel draining ranges. @feldgendler you can try the fix and let me know if it is sufficiently precise for you.
Yes, and yes
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
@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.