Skip to content
Permalink
Browse files

Fix: ignore return statements in dead code (fixed #11647)

  • Loading branch information...
mysticatea committed May 8, 2019
1 parent e4a08ba commit 4fd7cc51991e31f149c4fcbe08667cc5cc6049cc
Showing with 20 additions and 6 deletions.
  1. +8 −1 lib/rules/no-useless-return.js
  2. +12 −5 tests/lib/rules/no-useless-return.js
@@ -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;
}

@@ -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);
}
`
],

@@ -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 4fd7cc5

Please sign in to comment.
You can’t perform that action at this time.