Modify S1862: LaYC format (#2311)

This commit is contained in:
Gregory Paidis 2023-06-28 13:52:34 +02:00 committed by GitHub
parent abf1b0fea9
commit 8b7ef24bbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 27 deletions

View File

@ -1,3 +1,3 @@
{
}
"quickfix": "infeasible"
}

View File

@ -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[]

View File

@ -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.

View File

@ -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"
}

View File

@ -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[]