From 08247724edc3980440385bc75df31f3eba6527c5 Mon Sep 17 00:00:00 2001 From: Sebastien Marichal Date: Mon, 23 Dec 2024 15:06:00 +0100 Subject: [PATCH] NET-913 Modify rule S1264: Improve description to match the implementation --- rules/S1264/csharp/rule.adoc | 64 +++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/rules/S1264/csharp/rule.adoc b/rules/S1264/csharp/rule.adoc index fe598e7f71a..1154ad37173 100644 --- a/rules/S1264/csharp/rule.adoc +++ b/rules/S1264/csharp/rule.adoc @@ -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[]