no-else-return
Disallow else
blocks after return
statements in if
statements
Some problems reported by this rule are automatically fixable by the --fix
command line option
This rule is currently frozen and is not accepting changes.
If an if
block contains a return
statement, the else
block becomes unnecessary. Its contents can be placed outside of the block.
function foo() {
if (x) {
return y;
} else {
return z;
}
}
Rule Details
This rule is aimed at highlighting an unnecessary block of code following an if
containing a return statement. As such, it will warn when it encounters an else
following a chain of if
s, all of them containing a return
statement.
Options
This rule has an object option:
allowElseIf: true
(default) allowselse if
blocks after a returnallowElseIf: false
disallowselse if
blocks after a return
allowElseIf: true
Examples of incorrect code for this rule:
/*eslint no-else-return: "error"*/
function foo1() {
if (x) {
return y;
} else
}
function foo2() {
if (x) {
return y;
} else if (z) {
return w;
} else
}
function foo3() {
if (x) {
return y;
} else
return t;
}
function foo4() {
if (error) {
return 'It failed';
} else
}
// Two warnings for nested occurrences
function foo5() {
if (x) {
if (y) {
return y;
} else
} else
}
Examples of correct code for this rule:
/*eslint no-else-return: "error"*/
function foo1() {
if (x) {
return y;
}
return z;
}
function foo2() {
if (x) {
return y;
} else if (z) {
var t = "foo";
} else {
return w;
}
}
function foo3() {
if (x) {
if (z) {
return y;
}
} else {
return z;
}
}
function foo4() {
if (error) {
return 'It failed';
} else if (loading) {
return "It's still loading";
}
}
allowElseIf: false
Examples of incorrect code for this rule:
/*eslint no-else-return: ["error", {allowElseIf: false}]*/
function foo() {
if (error) {
return 'It failed';
} else
}
Examples of correct code for this rule:
/*eslint no-else-return: ["error", {allowElseIf: false}]*/
function foo() {
if (error) {
return 'It failed';
}
if (loading) {
return "It's still loading";
}
}
Version
This rule was introduced in ESLint v0.0.9.