Skip to content
← Back to rules

unicorn/no-lonely-if Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

What it does ​

Disallow if statements as the only statement in if blocks without else.

Why is this bad? ​

It can be confusing to have an if statement without an else clause as the only statement in an if block.

Examples ​

Examples of incorrect code for this rule:

javascript
if (foo) {
  if (bar) {
  }
}
if (foo) if (bar) baz();

Examples of correct code for this rule:

javascript
if (foo && bar) {
}
if (foo && bar) baz();

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-lonely-if": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-lonely-if": "error",
  },
});
bash
oxlint --deny unicorn/no-lonely-if

Version ​

This rule was added in v0.0.18.

References ​