Back to Biomejs

noUnusedLabels

src/content/docs/linter/rules/no-unused-labels.mdx

latest4.1 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/noUnusedLabels`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`no-unused-labels`](https://eslint.org/docs/latest/rules/no-unused-labels)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"noUnusedLabels": "error"
			}
		}
	}
}

Description

Disallow unused labels.

Labels that are declared and never used are most likely an error due to incomplete refactoring.

The rule ignores reactive Svelte statements in Svelte components.

Examples

Invalid

js
LOOP: for (const x of xs) {
    if (x > 0) {
        break;
    }
    f(x);
}
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-unused-labels">lint/correctness/noUnusedLabels</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Unused </span><span style="color: Orange;"><strong>label</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>LOOP: for (const x of xs) &#123; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> if (x &gt; 0) &#123; <strong>3 │ </strong> break; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The label is not used by any </span><span style="color: lightgreen;"><strong>break</strong></span><span style="color: lightgreen;"> statement and continue statement.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove the unused </span><span style="color: lightgreen;"><strong>label</strong></span><span style="color: lightgreen;">.</span> <strong> 1 │ </strong><span style="color: Tomato;">L</span><span style="color: Tomato;">O</span><span style="color: Tomato;">O</span><span style="color: Tomato;">P</span><span style="color: Tomato;">:</span><span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span>for<span style="opacity: 0.8;">·</span>(const<span style="opacity: 0.8;">·</span>x<span style="opacity: 0.8;">·</span>of<span style="opacity: 0.8;">·</span>xs)<span style="opacity: 0.8;">·</span>&#123; <strong> │ </strong><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>

Valid

js
LOOP: for (const x of xs) {
    if (x > 0) {
        break LOOP;
    }
    f(x);
}
js
function nonNegative(n) {
    DEV: assert(n >= 0);
    return n;
}
svelte
<script>
$: { /* reactive block */ }
</script>
</TabItem> </Tabs>