]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow Unused Labels (no-unused-labels) |
2 | ||
3 | Labels that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. | |
4 | ||
5 | ```js | |
6 | OUTER_LOOP: | |
7 | for (const student of students) { | |
8 | if (checkScores(student.scores)) { | |
9 | continue; | |
10 | } | |
11 | doSomething(student); | |
12 | } | |
13 | ``` | |
14 | ||
15 | In this case, probably removing `OUTER_LOOP:` had been forgotten. | |
16 | Such labels take up space in the code and can lead to confusion by readers. | |
17 | ||
18 | ## Rule Details | |
19 | ||
20 | This rule is aimed at eliminating unused labels. | |
21 | ||
22 | Examples of **incorrect** code for this rule: | |
23 | ||
24 | ```js | |
25 | /*eslint no-unused-labels: "error"*/ | |
26 | ||
27 | A: var foo = 0; | |
28 | ||
29 | B: { | |
30 | foo(); | |
31 | } | |
32 | ||
33 | C: | |
34 | for (let i = 0; i < 10; ++i) { | |
35 | foo(); | |
36 | } | |
37 | ``` | |
38 | ||
39 | Examples of **correct** code for this rule: | |
40 | ||
41 | ```js | |
42 | /*eslint no-unused-labels: "error"*/ | |
43 | ||
44 | A: { | |
45 | if (foo()) { | |
46 | break A; | |
47 | } | |
48 | bar(); | |
49 | } | |
50 | ||
51 | B: | |
52 | for (let i = 0; i < 10; ++i) { | |
53 | if (foo()) { | |
54 | break B; | |
55 | } | |
56 | bar(); | |
57 | } | |
58 | ``` | |
59 | ||
60 | ## When Not To Use It | |
61 | ||
62 | If you don't want to be notified about unused labels, then it's safe to disable this rule. | |
63 | ||
64 | ## Related Rules | |
65 | ||
66 | * [no-extra-label](./no-extra-label.md) | |
67 | * [no-labels](./no-labels.md) | |
68 | * [no-label-var](./no-label-var.md) |