Skip to content

Commit b03e54f

Browse files
authored
empty-block: Fix false positive on select {} (#805)
This fixes a false positive reported by revive on the following: select {} This is a way to block the program indefinitely. It is used in cases like: - Running a long-running server in a background thread and blocking `main` from exiting until the application dies. This is something you might do if your process has multiple servers/listeners in the same process. ```go go grpcListenAndServe() go httpListenAndServe() go logFlusher() select {} ``` - In programs compiled to WASM to prevent the application from exiting, so that the Javascript components may interact with it. ```go func main() { js.Global().Set("foo", foo) select {} } ``` Without this, one may see an error like, "Error: Go program has already exited" As a workaround, these programs can block forever by receiving from a throwaway channel (`<-make(chan struct{})`), but `select {}` is still a completely valid way of doing this, so supporting it makes sense. The issue was previously reported in #698 but was closed because the author was satisfied with a `//nolint` comment. Now that this rule is part of the default rule set (#799) the case for addressing the false positive is stronger. Resolves #804
1 parent 5db07b5 commit b03e54f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

rule/empty-block.go

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
4040
case *ast.FuncLit:
4141
w.ignore[n.Body] = true
4242
return w
43+
case *ast.SelectStmt:
44+
w.ignore[n.Body] = true
45+
return w
4346
case *ast.RangeStmt:
4447
if len(n.Body.List) == 0 {
4548
w.onFailure(lint.Failure{

testdata/empty-block.go

+13
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
package fixtures
44

5+
import "net/http"
6+
57
func f(x int) {} // Must not match
68

79
type foo struct{}
810

911
func (f foo) f(x *int) {} // Must not match
1012
func (f *foo) g(y *int) {} // Must not match
1113

14+
15+
func h() {
16+
go http.ListenAndServe()
17+
select {} // Must not match
18+
}
19+
1220
func g(f func() bool) {
1321
{ // MATCH /this block is empty, you can remove it/
1422
}
@@ -44,4 +52,9 @@ func g(f func() bool) {
4452
for range s { // MATCH /this block is empty, you can remove it/
4553
}
4654

55+
select {
56+
case _, ok := <-c:
57+
if ok { // MATCH /this block is empty, you can remove it/
58+
}
59+
}
4760
}

0 commit comments

Comments
 (0)