Please provide
- Operating system: Microsoft Windows 11 Enterprise
- Visual Studio version: 18.0.0
- SonarQube for Visual Studio plugin version: 8.30.0.15605
- Programming language you’re coding in: C# 12
- Is connected mode used: No
- SonarQube Cloud, SonarQube Server, or SonarQube Community Build? (if one of the latter two, which version?):
And a thorough description of the problem / question:
I’ll provide an example first:
public static string ExampleDigitsOnly(ReadOnlySpan<char> input)
{
Span<char> includeDigits = stackalloc char[input.Length];
for (int readIndex = 0, writeIndex = 0; readIndex < input.Length; readIndex++)
{
if (char.IsAsciiDigit(input[readIndex])) includeDigits[writeIndex++] = input[readIndex];
}
return new string(includeDigits);
}
Line 7: writeIndex gets tagged as violating RSPEC-127
The short hand description in editor is an exhortation to not update loop counter within loop body.
Well why not?
- The analyzer warning page itself however only speaks of not violating Stop condition counters.
- As can clearly be seen here, no stop counters are being touched or incremented.
I believe this is simply an overzealous implementation of an otherwise sensible rule to have (else why not just write it as a while loop), as altering other variables that are only used within the loop body is fine, the variable being declared in the for declaration should not impinge on that, but rather if it’s used in the stop condition or not.