Skip to content

Commit 5558566

Browse files
avorimachavacava
authored andcommitted
Prevent empty-block when looping over call expr
1 parent dc6909b commit 5558566

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

rule/empty-block.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
4343
case *ast.SelectStmt:
4444
w.ignore[n.Body] = true
4545
return w
46+
case *ast.ForStmt:
47+
if len(n.Body.List) == 0 && n.Init == nil && n.Post == nil && n.Cond != nil {
48+
if _, isCall := n.Cond.(*ast.CallExpr); isCall {
49+
w.ignore[n.Body] = true
50+
return w
51+
}
52+
}
4653
case *ast.RangeStmt:
4754
if len(n.Body.List) == 0 {
4855
w.onFailure(lint.Failure{

testdata/empty-block.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type foo struct{}
1111
func (f foo) f(x *int) {} // Must not match
1212
func (f *foo) g(y *int) {} // Must not match
1313

14-
1514
func h() {
1615
go http.ListenAndServe()
1716
select {} // Must not match
@@ -43,6 +42,10 @@ func g(f func() bool) {
4342

4443
}
4544

45+
for true { // MATCH /this block is empty, you can remove it/
46+
47+
}
48+
4649
// issue 386, then overwritten by issue 416
4750
var c = make(chan int)
4851
for range c { // MATCH /this block is empty, you can remove it/
@@ -57,4 +60,34 @@ func g(f func() bool) {
5760
if ok { // MATCH /this block is empty, you can remove it/
5861
}
5962
}
63+
64+
// issue 810
65+
next := 0
66+
iter := func(v *int) bool {
67+
*v = next
68+
next++
69+
fmt.Println(*v)
70+
return next < 10
71+
}
72+
73+
z := 0
74+
for iter(&z) { // Must not match
75+
}
76+
77+
for process() { // Must not match
78+
}
79+
80+
var it iterator
81+
for it.next() { // Must not match
82+
}
83+
}
84+
85+
func process() bool {
86+
return false
87+
}
88+
89+
type iterator struct{}
90+
91+
func (it *iterator) next() bool {
92+
return false
6093
}

0 commit comments

Comments
 (0)