Skip to content

Commit

Permalink
fix #386 by structurally identifying channel draining ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava committed Jul 8, 2021
1 parent daffd5f commit cecff17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions rule/empty-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
w.ignore[n.Body] = true
return w
case *ast.RangeStmt:
if n.Key == nil && n.Value == nil {
return nil // ignore this range (seems to be a channel draining iteration)
}

if len(n.Body.List) == 0 {
w.onFailure(lint.Failure{
Confidence: 0.9,
Expand Down
7 changes: 4 additions & 3 deletions testdata/empty-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ func g(f func() bool) {

}

// issue 386, then overwritten by issue 416
// issue 386
var c = make(chan int)
for range c { // MATCH /this block is empty, you can remove it/
for range c { // DO NOT FAIL
}

// But without types it skips this (too artificial?) one
var s = "a string"
for range s { // MATCH /this block is empty, you can remove it/
for range s { // DO NOT FAIL
}

}

0 comments on commit cecff17

Please sign in to comment.