noUnreachable (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v1.0.0 - Diagnostic Category:
lint/correctness/noUnreachable - This rule is recommended, meaning it is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Same as
no-unreachable
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "noUnreachable": "error" } } }}Description
Section titled “Description”Disallow code that can never run.
Statements after return, throw, break, or continue cannot execute. Unreachable code
is often left by mistake after a refactor and can hide intended behavior.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function example() { return; neverCalled();}code-block.js:3:5 lint/correctness/noUnreachable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This code will never be reached ...
1 │ function example() {
2 │ return;
> 3 │ neverCalled();
│ ^^^^^^^^^^^^^^
4 │ }
5 │
ℹ ... because this statement will return from the function beforehand
1 │ function example() {
> 2 │ return;
│ ^^^^^^^
3 │ neverCalled();
4 │ }
function example(condition) { while (condition) { break; neverCalled(); }}code-block.js:4:9 lint/correctness/noUnreachable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This code will never be reached ...
2 │ while (condition) {
3 │ break;
> 4 │ neverCalled();
│ ^^^^^^^^^^^^^^
5 │ }
6 │ }
ℹ ... because this statement will break the flow of the code beforehand
1 │ function example(condition) {
2 │ while (condition) {
> 3 │ break;
│ ^^^^^^
4 │ neverCalled();
5 │ }
function example() { for (const key in value) { continue; neverCalled(); }}code-block.js:4:9 lint/correctness/noUnreachable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This code will never be reached ...
2 │ for (const key in value) {
3 │ continue;
> 4 │ neverCalled();
│ ^^^^^^^^^^^^^^
5 │ }
6 │ }
ℹ ... because this statement will continue the loop beforehand
1 │ function example() {
2 │ for (const key in value) {
> 3 │ continue;
│ ^^^^^^^^^
4 │ neverCalled();
5 │ }
function example(condition) { if (condition) { return; } alwaysCalled();}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.