Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from May 11, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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" }

This comment has been minimized.

Copy link
@mysticatea

mysticatea May 8, 2019

Author Member

The rule now ignores unreachable code, so the second return; is just ignored.

]
output: "function foo() { return; }"
This conversation was marked as resolved by mysticatea

This comment has been minimized.

Copy link
@platinumazure

platinumazure May 8, 2019

Member

I think it would be good to have an errors object showing 1 error, and use the column option to show that the first return is the one flagged.

}
].map(invalidCase => Object.assign({ errors: [{ message: "Unnecessary return statement.", type: "ReturnStatement" }] }, invalidCase))
});
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.