Skip to content

Commit

Permalink
Fix: ignore return statements in dead code (fixes #11647)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed May 8, 2019
1 parent e4a08ba commit f9cfb38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/rules/no-useless-return.js
Expand Up @@ -255,7 +255,14 @@ module.exports = {
if (node.argument) {
markReturnStatementsOnCurrentSegmentsAsUsed();
}
if (node.argument || astUtils.isInLoop(node) || isInFinally(node)) {
if (
node.argument ||
astUtils.isInLoop(node) ||
isInFinally(node) ||

// Ignore `return` statements in unreachable places (https://github.com/eslint/eslint/issues/11647).
!scopeInfo.codePath.currentSegments.some(s => s.reachable)
) {
return;
}

Expand Down
17 changes: 12 additions & 5 deletions tests/lib/rules/no-useless-return.js
Expand Up @@ -165,6 +165,17 @@ ruleTester.run("no-useless-return", rule, {
throw new Error('foo');
while (false);
} catch (err) {}
`,

// https://github.com/eslint/eslint/issues/11647
`
function foo(arg) {
throw new Error("Debugging...");
if (!arg) {
return;
}
console.log(arg);
}
`
],

Expand Down Expand Up @@ -418,11 +429,7 @@ ruleTester.run("no-useless-return", rule, {
},
{
code: "function foo() { return; return; }",
output: "function foo() { return; }", // Other case is fixed in the second pass.
errors: [
{ message: "Unnecessary return statement.", type: "ReturnStatement" },
{ message: "Unnecessary return statement.", type: "ReturnStatement" }
]
output: "function foo() { return; }"
}
].map(invalidCase => Object.assign({ errors: [{ message: "Unnecessary return statement.", type: "ReturnStatement" }] }, invalidCase))
});

0 comments on commit f9cfb38

Please sign in to comment.