Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 770
user1042365 Avatar asked Mar 01 '26 20:03

user1042365


1 Answers

No warnings with while (i != 3).

Demo

like image 63
Jarod42 Avatar answered Mar 03 '26 13:03

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!