no-else-return

Disallow else after an if branch that always returns

fixable

When the if branch ends with return, the code after the if only runs if the condition was false. An else around that code adds a level of indentation and says nothing new.

--fix removes the else and moves its content out of the block, unless a name declared in the block would clash with one outside it.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-else-return" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-else-return" = ["error", {
  allowElseIf = true,
}]

Examples#

example.js1 problem
function label(user) {
  if (user.admin) {
    return "Admin";
  } else {
    return "Member";
  }
}
example.jsNo problems
function label(user) {
  if (user.admin) {
    return "Admin";
  }
  return "Member";
}

Options#

OptionDefaultWhat it does
allowElseIftrueAllow else if after a branch that returns.

By default an else if is fine:

example.jsNo problems
function label(user) {
  if (user.admin) {
    return "Admin";
  } else if (user.guest) {
    return "Guest";
  }
  return "Member";
}

The last else of a chain is still reported if every branch before it returns:

example.js1 problem
function label(user) {
  if (user.admin) {
    return "Admin";
  } else if (user.guest) {
    return "Guest";
  } else {
    return "Member";
  }
}

With allowElseIf: false, the else if is reported too:

example.jswith { "allowElseIf": false }1 problem
function label(user) {
  if (user.admin) {
    return "Admin";
  } else if (user.guest) {
    return "Guest";
  }
  return "Member";
}