Back to Devexpress

CRR0002 - Suspect variable reference in a For-loop condition

coderushforroslyn-118083-static-code-analysis-analyzers-library-crr0002-suspect-variable-reference-in-a-for-loop-condition.md

latest866 B
Original Source

CRR0002 - Suspect variable reference in a For-loop condition

  • Feb 28, 2025

This analyzer detects mistakes made in the for loop condition expression. For instance, the for loop can increment one variable, but test another. This may lead to incorrect number of iterations or even an infinite loop. It’s easy to make such mistake when editing a copy of another loop.

csharp
for (int i = 0; i < n1; i++) {
    for(int j = 0; i < n2; j++) { // CRR0002
        // ...
    }
}

In the example above, the inner loop should test the variable “ j “.

csharp
for (int i = 0; i < n1; i++) {
    for(int j = 0; j < n2; j++) {
        // ...
    }
}

See Also

Suppress Analyzers