Skip to content

Commit

Permalink
Prevent empty-block when looping over call expr
Browse files Browse the repository at this point in the history
  • Loading branch information
avorima authored and chavacava committed May 1, 2023
1 parent dc6909b commit 5558566
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions rule/empty-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
case *ast.SelectStmt:
w.ignore[n.Body] = true
return w
case *ast.ForStmt:
if len(n.Body.List) == 0 && n.Init == nil && n.Post == nil && n.Cond != nil {
if _, isCall := n.Cond.(*ast.CallExpr); isCall {
w.ignore[n.Body] = true
return w
}
}
case *ast.RangeStmt:
if len(n.Body.List) == 0 {
w.onFailure(lint.Failure{
Expand Down
35 changes: 34 additions & 1 deletion testdata/empty-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type foo struct{}
func (f foo) f(x *int) {} // Must not match
func (f *foo) g(y *int) {} // Must not match


func h() {
go http.ListenAndServe()
select {} // Must not match
Expand Down Expand Up @@ -43,6 +42,10 @@ func g(f func() bool) {

}

for true { // MATCH /this block is empty, you can remove it/

}

// issue 386, then overwritten by issue 416
var c = make(chan int)
for range c { // MATCH /this block is empty, you can remove it/
Expand All @@ -57,4 +60,34 @@ func g(f func() bool) {
if ok { // MATCH /this block is empty, you can remove it/
}
}

// issue 810
next := 0
iter := func(v *int) bool {
*v = next
next++
fmt.Println(*v)
return next < 10
}

z := 0
for iter(&z) { // Must not match
}

for process() { // Must not match
}

var it iterator
for it.next() { // Must not match
}
}

func process() bool {
return false
}

type iterator struct{}

func (it *iterator) next() bool {
return false
}

0 comments on commit 5558566

Please sign in to comment.