# no-lonely-if

Rule `unicorn/no-lonely-if`. Disallow `if` statements as the only statement in `if` blocks without `else`.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 12M weekly downloads of presets that enable it
- Trend: +40.1% last quarter
- Rank: #752 by reach, #948 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/no-lonely-if` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/no-lonely-if.md) |
| Oxlint | `unicorn/no-lonely-if` | no | no | plugin `unicorn`, category pedantic | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/no-lonely-if.html) |
| Biome | `style/useCollapsedIf` | no | safe | group style, since 1.9.4 | [docs](https://biomejs.dev/linter/rules/use-collapsed-if/) |

## Code examples

### Incorrect: the rule reports this

```js
if (foo) {
  if (bar) {
    // …
  }
}
```

```js
if (foo) {
  // …
} else if (bar) {
  if (baz) {
    // …
  }
}
```

### Correct: the rule accepts this

```js
if (foo && bar) {
  // …
}
```

```js
if (foo) {
  // …
} else if (bar && baz) {
  // …
}
```

```js
if (foo) {
  // …
} else if (bar) {
  if (baz) {
    // …
  }
} else {
  // …
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/no-lonely-if": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/no-lonely-if": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "style": {
        "useCollapsedIf": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [eslint/no-lonely-if](https://lintradar.dev/rules/eslint/no-lonely-if.md): Disallow `if` statements as the only statement in `else` blocks
- [unicorn/numeric-separators-style](https://lintradar.dev/rules/unicorn/numeric-separators-style.md): Enforce the style of numeric separators by correctly grouping digits.
- [unicorn/prefer-array-some](https://lintradar.dev/rules/unicorn/prefer-array-some.md): Prefer `.some(…)` over `.filter(…).length` check and `.{find,findLast,findIndex,findLastIndex}(…)`.
- [unicorn/prefer-at](https://lintradar.dev/rules/unicorn/prefer-at.md): Prefer `.at()` method for index access and `String#charAt()`.
- [unicorn/prefer-module](https://lintradar.dev/rules/unicorn/prefer-module.md): Prefer JavaScript modules (ESM) over CommonJS.
- [unicorn/prefer-regexp-test](https://lintradar.dev/rules/unicorn/prefer-regexp-test.md): Prefer `RegExp#test()` over `String#match()`, `String#search()`, and `RegExp#exec()`.

---

Web page: https://lintradar.dev/rules/unicorn/no-lonely-if. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
