Consider this code: void foo(void) { for (int i = 0; i < 2; i++); } Compiling the above code with -std=c99 -Wdeclaration-after-statement does not produce a warning as expected.
Yup. Also fails for me with GCC 4.3.2 and GCC 4.1.1. I don't have anything older available.
I tested and found it failing with 3.4.6, 4.1.2, 4.2.4, 4.3.6, 4.4.6, 4.5.3, and 4.6.2.
I think -Wdeclaration-after-statement would the wrong option really. The declaration in "for (int i = 0; i < 2; i++)" is not after a statement. I would think we should have a different warning option for this. Also the warning option is there mostly to warn as we turn on this extension by default for GNU C90 while the declaration inside a for loop support is not turned on for GNU C90.
I'm not here to argue semantics. I use -Wdeclaration-after-statement to avoid accidentally introducing code that will fail with compilers that do not support this. Having an equivalent for the for loop construct would have saved me some time today. If all compilers supported C99 fully, there would be no need for any of this, but sadly this is not the case.
I agree that a separate warning option should be used here.
This would be easy to implement, if it's still wanted. I suggest the name "-Wdecl-in-for-loop".
This is now warning with -Wc90-c99-compat (since GCC 9). Though it does not have its own option.