Skip to content

Commit

Permalink
Fix: ignore return statements in dead code (fixes #11647) (#11688)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and not-an-aardvark committed May 11, 2019
1 parent aae6f65 commit 9484e9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/rules/no-useless-return.js
Original file line number Diff line number Diff line change
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
22 changes: 17 additions & 5 deletions tests/lib/rules/no-useless-return.js
Original file line number Diff line number Diff line change
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,12 @@ 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; }",
errors: [{
message: "Unnecessary return statement.",
type: "ReturnStatement",
column: 18
}]
}
].map(invalidCase => Object.assign({ errors: [{ message: "Unnecessary return statement.", type: "ReturnStatement" }] }, invalidCase))
});

0 comments on commit 9484e9e

Please sign in to comment.