From 8b7ef24bbe81b587ebeaff4cc126dbb0f7b4bc6f Mon Sep 17 00:00:00 2001 From: Gregory Paidis <115458417+gregory-paidis-sonarsource@users.noreply.github.com> Date: Wed, 28 Jun 2023 13:52:34 +0200 Subject: [PATCH] Modify S1862: LaYC format (#2311) --- rules/S1862/csharp/metadata.json | 4 ++-- rules/S1862/csharp/rule.adoc | 35 +++++++++++++++++++++++--------- rules/S1862/description.adoc | 3 +-- rules/S1862/vbnet/metadata.json | 5 +++-- rules/S1862/vbnet/rule.adoc | 33 ++++++++++++++++++++---------- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/rules/S1862/csharp/metadata.json b/rules/S1862/csharp/metadata.json index 1797133380..69c47cb7d6 100644 --- a/rules/S1862/csharp/metadata.json +++ b/rules/S1862/csharp/metadata.json @@ -1,3 +1,3 @@ { - -} + "quickfix": "infeasible" +} \ No newline at end of file diff --git a/rules/S1862/csharp/rule.adoc b/rules/S1862/csharp/rule.adoc index d4f54ae7a1..12f7e5f37d 100644 --- a/rules/S1862/csharp/rule.adoc +++ b/rules/S1862/csharp/rule.adoc @@ -1,10 +1,17 @@ == Why is this an issue? -include::../description.adoc[] +A chain of https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-if-statement[if/else if] statements is evaluated from top to bottom. At most, only one branch will be executed: the first statement with a condition that evaluates to `true`. +Therefore, duplicating a condition leads to unreachable code inside the duplicated condition block. Usually, this is due to a copy/paste error. -=== Noncompliant code example +The result of such duplication can lead to unreachable code or even to unexpected behavior. -[source,csharp] +== How to fix it + +=== Code examples + +==== Noncompliant code example + +[source,csharp,diff-id=1,diff-type=noncompliant] ---- if (param == 1) { @@ -14,15 +21,15 @@ else if (param == 2) { CloseWindow(); } -else if (param == 1) // Noncompliant +else if (param == 1) // Noncompliant: condition has already been checked { - MoveWindowToTheBackground(); + MoveWindowToTheBackground(); // unreachable code } ---- -=== Compliant solution +==== Compliant solution -[source,csharp] +[source,csharp,diff-id=1,diff-type=compliant] ---- if (param == 1) { @@ -38,14 +45,22 @@ else if (param == 3) } ---- +== Resources -ifdef::env-github,rspecator-view[] +=== Documentation + +* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-if-statement[The if statement] + + +ifdef::env-github,rspecator-view,env-vscode[] ''' == Implementation Specification (visible only on this page) -include::../message.adoc[] +=== Message + +This branch duplicates the one on line n. include::../highlighting.adoc[] @@ -61,4 +76,4 @@ include::../highlighting.adoc[] include::../comments-and-links.adoc[] -endif::env-github,rspecator-view[] +endif::env-github,rspecator-view,env-vscode[] diff --git a/rules/S1862/description.adoc b/rules/S1862/description.adoc index 13ad2c73bd..ff766586e0 100644 --- a/rules/S1862/description.adoc +++ b/rules/S1862/description.adoc @@ -1,4 +1,3 @@ -A chain of ``++if++``/``++else if++`` statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to ``++true++``. - +A chain of `if`/`else if` statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to `true`. Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it's simply dead code and at worst, it's a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior. diff --git a/rules/S1862/vbnet/metadata.json b/rules/S1862/vbnet/metadata.json index b1c1436c08..2245b99f25 100644 --- a/rules/S1862/vbnet/metadata.json +++ b/rules/S1862/vbnet/metadata.json @@ -1,3 +1,4 @@ { - "title": "Related \"If\/ElseIf\" statements should not have the same condition" -} + "title": "Related \"If\/ElseIf\" statements should not have the same condition", + "quickfix": "infeasible" +} \ No newline at end of file diff --git a/rules/S1862/vbnet/rule.adoc b/rules/S1862/vbnet/rule.adoc index d6197c188d..b62790a2e6 100644 --- a/rules/S1862/vbnet/rule.adoc +++ b/rules/S1862/vbnet/rule.adoc @@ -1,26 +1,30 @@ == Why is this an issue? -A chain of ``++If++``/``++ElseIf++`` statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to ``++True++``. +A chain of https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If/ElseIf] statements is evaluated from top to bottom. At most, only one branch will be executed: the first statement with a condition that evaluates to `True`. +Therefore, duplicating a condition leads to unreachable code inside the duplicated condition block. Usually, this is due to a copy/paste error. +The result of such duplication can lead to unreachable code or even to unexpected behavior. -Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it's simply dead code and at worst, it's a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior. +== How to fix it -=== Noncompliant code example +=== Code examples -[source,vbnet] +==== Noncompliant code example + +[source,vbnet,diff-id=1,diff-type=noncompliant] ---- If param = 1 Then OpenWindow() ElseIf param = 2 Then CloseWindow() -ElseIf param = 1 Then ' Noncompliant - MoveWindowToTheBackground() +ElseIf param = 1 Then ' Noncompliant: condition has already been checked + MoveWindowToTheBackground() ' unreachable code End If ---- -=== Compliant solution +==== Compliant solution -[source,vbnet] +[source,vbnet,diff-id=1,diff-type=compliant] ---- If param = 1 Then OpenWindow() @@ -31,14 +35,21 @@ ElseIf param = 3 Then End If ---- +== Resources -ifdef::env-github,rspecator-view[] +=== Documentation + +* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If...Then...Else Statement] + +ifdef::env-github,rspecator-view,env-vscode[] ''' == Implementation Specification (visible only on this page) -include::../message.adoc[] +=== Message + +This branch duplicates the one on line n. include::../highlighting.adoc[] @@ -48,4 +59,4 @@ include::../highlighting.adoc[] include::../comments-and-links.adoc[] -endif::env-github,rspecator-view[] +endif::env-github,rspecator-view,env-vscode[]