An if, while, for, etc does not "own" the { and }. It merely expects a valid statement.
; is a valid statement.
moveServoToPosition(90); is a valid statement.
{ moveServoToPosition(90); } is a valid statement.
{ } is a valid statement.
The { and } merely convert the containing statements into a single compound statement as seen by the parent layer (the if, while, whatever).
While I agree that an if statement with no body (if(x==3) is syntactically correct, it is pointless, and yes a warning "If without body" could be generated. However, we're not arguing about ifs here - if was introduced about half way through the discussion by WizenedEE.
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.
There is no way for a compiler to know what a programmer intends.
If you want to start forcing a { } to indicate an intentional empty body, then two things are happening:
-
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.
-
It will break the C specification, which states that the above code is valid.
And then, will you warn about:
for(x=0; x<10; x++)
doSomething(x);
just because the doSomething(x) isn't inside a { and } ?
This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.
None of the above three chunks of code are in any way questionable, vague, or otherwise suspect. They are perfectly valid.
Modifying them to "prevent the warning" involves things like adding extra brackets around assignments, etc - not changing the whole structure of the C language!