close

no-else-return

Configuration

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

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

Rule Details

Disallow else blocks after return statements in if statements. When every preceding branch returns, the else block is unnecessary and its contents can be placed after the if statement.

Examples of incorrect code for this rule:

function foo() {
  if (x) {
    return y;
  } else {
    return z;
  }
}
function foo() {
  if (x) {
    return y;
  } else if (z) {
    return w;
  } else {
    return q;
  }
}

Examples of correct code for this rule:

function foo() {
  if (x) {
    return y;
  }

  return z;
}
function foo() {
  if (x) {
    doSomething();
  } else {
    return y;
  }
}

Options

This rule has an object option:

  • allowElseIf (default: true): allows else if blocks after a return.

Examples of correct code for this rule with { "allowElseIf": true }:

{ "no-else-return": ["error", { "allowElseIf": true }] }
function foo() {
  if (error) {
    return "failed";
  } else if (loading) {
    return "loading";
  }
}

Examples of incorrect code for this rule with { "allowElseIf": false }:

{ "no-else-return": ["error", { "allowElseIf": false }] }
function foo() {
  if (error) {
    return "failed";
  } else if (loading) {
    return "loading";
  }
}

Original Documentation

https://eslint.org/docs/latest/rules/no-else-return