Index

no-useless-return

禁止冗余返回语句

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

一个 return; 语句如果后面没有任何内容是多余的,并且对函数的运行时行为没有影响。这可能会让人感到困惑,因此最好禁止这些多余的语句。

🌐 A return; statement with nothing after it is redundant, and has no effect on the runtime behavior of a function. This can be confusing, so it’s better to disallow these redundant statements.

规则详情

🌐 Rule Details

此规则旨在报告冗余的 return 语句。

🌐 This rule aims to report redundant return statements.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/* eslint no-useless-return: "error" */

const foo = function() { return; }

const bar = function() {
  doSomething();
  return;
}

const baz = function() {
  if (condition) {
    qux();
    return;
  } else {
    quux();
  }
}

const item = function() {
  switch (bar) {
    case 1:
      doSomething();
    default:
      doSomethingElse();
      return;
  }
}

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/* eslint no-useless-return: "error" */

const foo = function() { return 5; }

const bar = function() {
  return doSomething();
}

const baz = function() {
  if (condition) {
    qux();
    return;
  } else {
    quux();
  }
  qux();
}

const item = function() {
  switch (bar) {
    case 1:
      doSomething();
      return;
    default:
      doSomethingElse();
  }
}

const func = function() {
  for (const foo of bar) {
    return;
  }
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 When Not To Use It

如果你不关心不允许多余的 return 语句,你可以关闭此规则。

🌐 If you don’t care about disallowing redundant return statements, you can turn off this rule.

版本

此规则是在 ESLint v3.9.0 中引入。

资源