Skip to content

no-empty-block

Reports a block with no statement and no comment as the body of an if, else, loop, try or finally (if (a) { }, for (;;) { }). A comment inside counts as content. A bare { } and a function body are not reported, and an empty catch is the rule no-empty-catch. With comment_handles off a comment does not count. With waiting_loops an empty loop whose condition is a call is a wait and is left alone, and so is a loop whose head matches ignored_loop_heads.

An empty block is usually a branch that was started and never filled in, or code that was deleted around it. The reader cannot tell whether the nothing is meant.

To fix, write the code, or say in a comment that nothing is intended.

Reported
package a
func f(ok bool, ch chan int, g func() bool) {
if ok {
}
if ok {
// nothing
} else {
}
for ok {
}
for i := 0; i < 3; i++ {
}
for i := range ch {
}
for g() {
}
...
func empty() {}
func forever() {
for {
}
}
c:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
cpp:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
csharp:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
go:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
java:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
js:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
kotlin:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
php:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
python:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
ruby:
lint:
rules:
no-empty-block:
level: warn # off, warn (default), error
options:
comment_handles: true
waiting_loops: false
ignored_loop_heads: "\\b\\B"
Option Type Default Description
comment_handles bool true A comment in the block counts as content
waiting_loops bool false Leave alone an empty loop whose condition is a call
ignored_loop_heads string "\\b\\B" A regular expression for the condition or head of a loop whose empty body is left alone

Enabled by the presets: go/revive, js/eslint-recommended.

Suppress one line with sudsy-disable-next-line no-empty-block in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint no-empty partial the allowEmptyCatch option is not supported
revive empty-block partial a bare {} block statement and an empty switch body are not read