diff --git a/rules/S1763/apex/rule.adoc b/rules/S1763/apex/rule.adoc index 59761169303..0a2b8e35fbb 100644 --- a/rules/S1763/apex/rule.adoc +++ b/rules/S1763/apex/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++return++``, ``++break++``, ``++continue++``) and ``++throw++`` expressions move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/cfamily/rule.adoc b/rules/S1763/cfamily/rule.adoc index ce4dc80bc56..8a18c766f91 100644 --- a/rules/S1763/cfamily/rule.adoc +++ b/rules/S1763/cfamily/rule.adoc @@ -1,12 +1,79 @@ +include::../summary.adoc[] + == Why is this an issue? -Some statements (`return`, `break`, `continue`, `goto`, `co_return`) and `throw` expressions move control flow out of the current code block. Furthermore, some function do not return control flow (e.g. `abort()`, `std::terminate()`, functions with the ``++[[noreturn]]++`` attribute). +Some statements and expressions move the control flow out of the current code block. +Additionally, some functions never return the control flow to the caller. +Any unlabeled statements that come after such a jump or function call is unreachable. + +For instance, within a code block, code following a statement containing any of these keywords +is effectively dead code: + +1. `return` +2. `break` +3. `continue` +4. `goto` +5. `co_return` +6. `throw` + +Examples of functions that never return the control flow to the caller: + +1. `exit()` +2. `abort()` +3. `std::terminate()` +4. Functions with the ``++[[noreturn]]++`` attribute. + + + +== How to fix it + +The affected code block should be checked to verify the intended +behavior, and the logic should be corrected, or the dead code removed. + +=== Code examples + +==== Noncompliant code example + +[source,cpp,diff-id=1,diff-type=noncompliant] +---- +int fun(int a) { + int i = 10; + return i + a; // Noncompliant: There are following statements within the code block + i++; // Dead code +} +---- + +[source,cpp,diff-id=2,diff-type=noncompliant] +---- +int divide(int a, int b) { + if (b == 0) { + abort(); // Noncompliant: `abort()` never returns to the caller + std::cerr << "Divisor is 0!" << std::endl; // Dead code + } + return a / b; +} +---- -Any unlabeled statements that come after such a jump or function call are unreachable, and either this dead code should be removed, or the logic should be corrected. +==== Compliant solution -include::../noncompliant.adoc[] +[source,cpp,diff-id=1,diff-type=compliant] +---- +int fun(int a) { + int i = 10; + return i + a; +} +---- -include::../compliant.adoc[] +[source,cpp,diff-id=2,diff-type=compliant] +---- +int divide(int a, int b) { + if (b == 0) { + std::cerr << "Divisor is 0!" << std::endl; + abort(); + } + return a / b; +} +---- == Resources diff --git a/rules/S1763/compliant.adoc b/rules/S1763/compliant.adoc deleted file mode 100644 index adff7b34468..00000000000 --- a/rules/S1763/compliant.adoc +++ /dev/null @@ -1,9 +0,0 @@ -=== Compliant solution - -[source,text] ----- -int fun(int a) { - int i = 10; - return i + a; -} ----- diff --git a/rules/S1763/go/rule.adoc b/rules/S1763/go/rule.adoc index 3bcad4735f0..a4d5f7f7384 100644 --- a/rules/S1763/go/rule.adoc +++ b/rules/S1763/go/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? include::../description.adoc[] diff --git a/rules/S1763/kotlin/rule.adoc b/rules/S1763/kotlin/rule.adoc index 8df4e6c8936..68e85fc22c3 100644 --- a/rules/S1763/kotlin/rule.adoc +++ b/rules/S1763/kotlin/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++return++``, ``++break++`` and ``++continue++``) move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/noncompliant.adoc b/rules/S1763/noncompliant.adoc deleted file mode 100644 index b1a92f27517..00000000000 --- a/rules/S1763/noncompliant.adoc +++ /dev/null @@ -1,10 +0,0 @@ -=== Noncompliant code example - -[source,text] ----- -int fun(int a) { - int i = 10; - return i + a; // Noncompliant - i++; // dead code -} ----- diff --git a/rules/S1763/php/rule.adoc b/rules/S1763/php/rule.adoc index 9b2af7b6325..edcbb8c7d80 100644 --- a/rules/S1763/php/rule.adoc +++ b/rules/S1763/php/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? include::../description.adoc[] diff --git a/rules/S1763/plsql/rule.adoc b/rules/S1763/plsql/rule.adoc index db44d0ef6f9..f065a0b145a 100644 --- a/rules/S1763/plsql/rule.adoc +++ b/rules/S1763/plsql/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++EXIT++``, ``++CONTINUE++``, ``++RETURN++``, ``++RAISE++``, and ``++RAISE_APPLICATION_ERROR++``), move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/python/rule.adoc b/rules/S1763/python/rule.adoc index c3ddd3d9c28..2e36af0a701 100644 --- a/rules/S1763/python/rule.adoc +++ b/rules/S1763/python/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++return++``, ``++break++``, ``++continue++``, and ``++raise++``) move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/ruby/rule.adoc b/rules/S1763/ruby/rule.adoc index 6133828e1b1..3911695a98d 100644 --- a/rules/S1763/ruby/rule.adoc +++ b/rules/S1763/ruby/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++return++``, ``++break++`` and ``++next++``) move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/rule.adoc b/rules/S1763/rule.adoc deleted file mode 100644 index 7f48b2b9fd7..00000000000 --- a/rules/S1763/rule.adoc +++ /dev/null @@ -1,9 +0,0 @@ -== Why is this an issue? - -include::description.adoc[] - -include::noncompliant.adoc[] - -include::compliant.adoc[] - -include::see.adoc[] diff --git a/rules/S1763/scala/rule.adoc b/rules/S1763/scala/rule.adoc index a4cb05c6e15..3fe080c2456 100644 --- a/rules/S1763/scala/rule.adoc +++ b/rules/S1763/scala/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++return++``) move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/summary.adoc b/rules/S1763/summary.adoc new file mode 100644 index 00000000000..dd07f98ad39 --- /dev/null +++ b/rules/S1763/summary.adoc @@ -0,0 +1 @@ +Once control flow has been moved out of the current code block, any subsequent statements become effectively unreachable. diff --git a/rules/S1763/swift/rule.adoc b/rules/S1763/swift/rule.adoc index 488ba6afa6e..0dd628f4949 100644 --- a/rules/S1763/swift/rule.adoc +++ b/rules/S1763/swift/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++return++``, ``++break++``, ``++continue++``, and ``++fallthrough++``) move control flow out of the current code block. So any statements that come after a jump are dead code. diff --git a/rules/S1763/tsql/rule.adoc b/rules/S1763/tsql/rule.adoc index efb06ad91dc..6a56b79938a 100644 --- a/rules/S1763/tsql/rule.adoc +++ b/rules/S1763/tsql/rule.adoc @@ -1,3 +1,5 @@ +include::../summary.adoc[] + == Why is this an issue? Jump statements (``++BREAK++``, ``++CONTINUE++``, ``++RETURN++``, ``++GOTO++``, and ``++THROW++``), move control flow out of the current code block. So any statements that come after a jump are dead code.