From cecff176a4597695cdec4989fccf917452747737 Mon Sep 17 00:00:00 2001 From: chavacava Date: Thu, 8 Jul 2021 19:05:58 +0000 Subject: [PATCH] fix #386 by structurally identifying channel draining ranges --- rule/empty-block.go | 4 ++++ testdata/empty-block.go | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/rule/empty-block.go b/rule/empty-block.go index fbec4d93c..b50ad9d15 100644 --- a/rule/empty-block.go +++ b/rule/empty-block.go @@ -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, diff --git a/testdata/empty-block.go b/testdata/empty-block.go index d75015757..2da6333dc 100644 --- a/testdata/empty-block.go +++ b/testdata/empty-block.go @@ -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 } }