Search code examples
c++clangwarnings

Clang invalid -Wfor-loop-analysis


Clang (version 17.0.6) complains: warning: variable 'i' used in loop condition not modified in loop body [-Wfor-loop-analysis]

on this code:

int i = 0;
auto add = [&]
    {
        cout << i;
        i++;
    };
for (; i != 3; )
    add();

Because clang can't realize that i is actually changed.

If you are wondering, the reason I don't modify i++ in the for loop statement is because this add function is reused later.

Should I just disable this warning?


Solution

  • No warnings with while (i != 3).

    Demo