Eslint
规则 | Rules

no-useless-return

不允许多余的返回语句(无用的返回)

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

没有任何内容的return;语句是多余的,并且对函数的运行时行为没有影响。这可能会造成混淆,因此最好禁止这些冗余语句。

规则细节

这条规则旨在报告多余的return语句。

此规则的错误代码示例:

/* eslint no-useless-return: "error" */ function foo() { return; } function foo() { doSomething( return; } function foo() { if (condition) { bar( return; } else { baz( } } function foo() { switch (bar) { case 1: doSomething( default: doSomethingElse( return; } }

此规则的正确代码示例:

/* eslint no-useless-return: "error" */ function foo() { return 5; } function foo() { return doSomething( } function foo() { if (condition) { bar( return; } else { baz( } qux( } function foo() { switch (bar) { case 1: doSomething( return; default: doSomethingElse( } } function foo() { for (const foo of bar) { return; } }

何时不使用它

如果你不关心不允许多余的返回语句,你可以关闭这条规则。

版本

这条规则是在 ESLint 3.9.0中引入的。

资源