47 lines
877 B
Plaintext
47 lines
877 B
Plaintext
![]() |
Jump statements (<code>return</code>, <code>break</code> and <code>continue</code>) and <code>throw</code> expressions move control flow out of the current code block. So any statements that come after a jump are dead code.
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
function fun(a) {
|
||
|
var i = 10;
|
||
|
return i + a;
|
||
|
i++; // Noncompliant; this is never executed
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
function fun(int a) {
|
||
|
var i = 10;
|
||
|
return i + a;
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Exceptions
|
||
|
|
||
|
This rule ignores unreachable <code>break</code> statements in <code>switch</code> clauses.
|
||
|
|
||
|
----
|
||
|
switch (x) {
|
||
|
case 42:
|
||
|
return 43;
|
||
|
break; // Compliant
|
||
|
default:
|
||
|
doSomething();
|
||
|
}
|
||
|
----
|
||
|
|
||
|
Hoisted variables declarations without initialization are always considered reachable.
|
||
|
----
|
||
|
function bar() {
|
||
|
return x = function() {
|
||
|
x.foo = 42;
|
||
|
}
|
||
|
var x;
|
||
|
}
|
||
|
----
|
||
|
|
||
|
include::../see.adoc[]
|