no-empty

Disallow empty block statements

warn by defaulthas suggestions

An empty block is usually code that was never finished, or something a refactor left behind. An empty catch is worse, because the error disappears and nothing tells you that the call failed.

A block with a comment in it isn't reported, so you can say why it's empty. Empty function bodies aren't reported either. Use no-empty-function for those.

This rule is on by default.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"no-empty" = "off" # or "error"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-empty" = ["warn", {
  allowEmptyCatch = false,
}]

Examples#

example.js1 problem
try {
  cache.clear();
} catch (error) {}
example.js2 problems
if (user.active) {
}

switch (user.role) {
}
example.jsNo problems
try {
  cache.clear();
} catch (error) {
  // The cache is optional. A failure here changes nothing.
}

function noop() {}

Options#

OptionDefaultWhat it does
allowEmptyCatchfalseAllow an empty catch block.
example.jswith { "allowEmptyCatch": true }No problems
try {
  cache.clear();
} catch (error) {}

Other empty blocks are still reported, including the finally of the same statement:

example.jswith { "allowEmptyCatch": true }1 problem
try {
  cache.clear();
} catch (error) {
} finally {
}