Skip to content

Commit

Permalink
NET-913 Modify rule S1264: Improve description to match the implement…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
sebastien-marichal committed Dec 23, 2024
1 parent bb47c97 commit 0824772
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion rules/S1264/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
include::../rule.adoc[]
== Why is this an issue?

Using a `for` loop without its typical structure (initialization, condition, increment) can be confusing. Otherswise, it is better to use a `while` loop in this case, as it is more readable.

The initializer section should contain a variable declaration to be considered as a valid initialization.

== How to fix it

Replace the `for` loop with a `while` loop.

=== Code example

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
for (;condition;) // Noncompliant; both the initializer and increment sections are missing
{
// Do something
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
while (condition)
{
// Do something
}
----

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
int i;
for (i = 0; i < 10;) // Noncompliant; the initializer section should contain a variable declaration
{
// Do something
i++;
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
int i = 0;
while (i < 10)
{
// Do something
i++;
}
----

== Resources

=== Documentation

* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement[The `for` statement]

ifdef::env-github,rspecator-view[]

Expand Down

0 comments on commit 0824772

Please sign in to comment.