]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-useless-return.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-useless-return.md
1 ---
2 title: no-useless-return
3 layout: doc
4 rule_type: suggestion
5 ---
6
7
8
9 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.
10
11 ## Rule Details
12
13 This rule aims to report redundant `return` statements.
14
15 Examples of **incorrect** code for this rule:
16
17 ::: incorrect
18
19 ```js
20 /* eslint no-useless-return: "error" */
21
22 function foo() { return; }
23
24 function foo() {
25 doSomething();
26 return;
27 }
28
29 function foo() {
30 if (condition) {
31 bar();
32 return;
33 } else {
34 baz();
35 }
36 }
37
38 function foo() {
39 switch (bar) {
40 case 1:
41 doSomething();
42 default:
43 doSomethingElse();
44 return;
45 }
46 }
47
48 ```
49
50 :::
51
52 Examples of **correct** code for this rule:
53
54 ::: correct
55
56 ```js
57 /* eslint no-useless-return: "error" */
58
59 function foo() { return 5; }
60
61 function foo() {
62 return doSomething();
63 }
64
65 function foo() {
66 if (condition) {
67 bar();
68 return;
69 } else {
70 baz();
71 }
72 qux();
73 }
74
75 function foo() {
76 switch (bar) {
77 case 1:
78 doSomething();
79 return;
80 default:
81 doSomethingElse();
82 }
83 }
84
85 function foo() {
86 for (const foo of bar) {
87 return;
88 }
89 }
90
91 ```
92
93 :::
94
95 ## When Not To Use It
96
97 If you don't care about disallowing redundant return statements, you can turn off this rule.