> For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt.

# no-lonely-if

[Added in v0.8.2](https://github.com/web-infra-dev/rslint/releases/tag/v0.8.2)
## Configuration


```ts title=rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-lonely-if': 'error',
    },
  },
]);
```

## Rule Details

If an `if` statement is the only statement in the `else` block, it is often clearer to use an `else if` form.

```javascript
if (foo) {
    // ...
} else {
    if (bar) {
        // ...
    }
}
```

should be rewritten as

```javascript
if (foo) {
    // ...
} else if (bar) {
    // ...
}
```

This rule disallows `if` statements as the only statement in `else` blocks.

Examples of **incorrect** code for this rule:

```javascript
if (condition) {
    // ...
} else {
    if (anotherCondition) {
        // ...
    }
}

if (condition) {
    // ...
} else {
    if (anotherCondition) {
        // ...
    } else {
        // ...
    }
}
```

Examples of **correct** code for this rule:

```javascript
if (condition) {
    // ...
} else if (anotherCondition) {
    // ...
}

if (condition) {
    // ...
} else if (anotherCondition) {
    // ...
} else {
    // ...
}

if (condition) {
    // ...
} else {
    if (anotherCondition) {
        // ...
    }
    doSomething();
}
```

## When Not To Use It

Disable this rule if the code is clearer without requiring the `else if` form.

## Original Documentation

- [ESLint: no-lonely-if](https://eslint.org/docs/latest/rules/no-lonely-if)
- [Source code](https://github.com/eslint/eslint/blob/v10.8.1/lib/rules/no-lonely-if.js)
