2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-26 09:56:48 +02:00
Unreachable code is the code whose statements cannot be executed under any circumstances. Jump statements, like ``++return++``, ``++break++``, ``++continue++``, and ``++throw++``, alter the normal flow of control within a program, making it possible to skip certain parts of the code, terminate loops prematurely, or exit from functions. So any statements that come after a jump are effectively unreachable.
2020-06-30 12:47:33 +02:00
2023-07-26 09:56:48 +02:00
Unreachable statements can be a sign of a logical error or oversight in the program's design, leading to unexpected behavior at runtime.
2020-06-30 12:47:33 +02:00
2023-07-26 09:56:48 +02:00
[source,javascript,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
2023-07-26 09:56:48 +02:00
function func(a) {
let i = 10;
2020-06-30 12:47:33 +02:00
return i + a;
2023-07-26 09:56:48 +02:00
i++; // Noncompliant: this is never executed
2020-06-30 12:47:33 +02:00
}
----
2023-07-26 09:56:48 +02:00
Identify and remove unreachable statements from your code.
2020-06-30 12:47:33 +02:00
2023-07-26 09:56:48 +02:00
[source,javascript,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
2023-07-26 09:56:48 +02:00
function func(a) {
let i = 10;
2020-06-30 12:47:33 +02:00
return i + a;
}
----
2023-07-26 09:56:48 +02:00
== Resources
=== Documentation
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return[MDN - `return`]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break[MDN - `break`]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw[MDN - `throw`]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue[MDN - `continue`]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Stmt_after_return[MDN - Warning: unreachable code after return statement]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this code after the "[return|break|continue|goto|throw]" statement.
2021-09-20 15:38:42 +02:00
include::../highlighting.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]