
Co-authored-by: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com>
55 lines
1.8 KiB
Plaintext
55 lines
1.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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.
|
|
|
|
Unreachable statements can be a sign of a logical error or oversight in the program's design, leading to unexpected behavior at runtime.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
function func(a) {
|
|
let i = 10;
|
|
return i + a;
|
|
i++; // Noncompliant: this is never executed
|
|
}
|
|
----
|
|
|
|
Identify and remove unreachable statements from your code.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
function func(a) {
|
|
let i = 10;
|
|
return i + a;
|
|
}
|
|
----
|
|
|
|
== 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]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this code after the "[return|break|continue|goto|throw]" statement.
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|