name lookup of 'i' changed for new ISO 'for' scoping

majenko:
The code

for(ptr=head; ptr->next; ptr++);

and the code

for (int i = 1500; i >= 1000; i --);

are both perfectly syntactically correct, and in some circumstances are programatically correct as well - i.e., they are exactly what the programmer intended.

Whilst I agree that those examples are not quite in the same category as "if (x = 8)", I think the fact that the OP made the mistake of putting in that semicolon and had to ask for help to find it demonstrates the usefulness of a compiler warning for this construct.

majenko:
If you want to start forcing a { } to indicate an intentional empty body, then two things are happening:

  1. You are forcing a coding style on someone. This is not something that C does. Python imposes coding styles. COBOL imposes coding styles. Upper management impose coding styles. C doesn't, it never has, and it never will.

Anyone who writes commercial C code without a coding style and a tool that enforces that style (even if that tool is just the compiler with extra warnings enabled) is IMO stupid and irresponsible. But Arduino is intended for hobbyists and novices, so the situation here is less clear cut. I would like the Arduino IDE to provide a choice of warning levels, or some way of passing additional flags to gcc.

majenko:
2. It will break the C specification, which states that the above code is valid.

Warning about questionable code does not break the C specification. Refusing to compile it would.

majenko:
None of the above three chunks of code are in any way questionable, vague, or otherwise suspect. They are perfectly valid.

We'll have to agree to differ about that.

majenko:
Modifying them to "prevent the warning" involves things like adding extra brackets around assignments, etc - not changing the whole structure of the C language!

Requiring the programmer to put the semicolon on a different line from the end of the loop header (in order to make his intention clear and suppress the warning) does not change the structure of the C language either. [Although all C coding standards for safety-critical software I have seen do make { } compulsory in the bodies of all loops and if-statements, in order to reduce the incidence of errors.]

BTW I believe gcc does warn about for-loops of this form if you use the -Wextra or -Wempty-body compiler options. It's not documented on the page I linked to, but I found a note that gcc extends that warning to include for-loops from gcc 4.3.