After answering Xcode- C programming - While loop, I stopped making theoric answers and installed
clang
gcc
-Wempty-body
clang
if
int main(void)
{
char c=1;
while (c);
--c;
if (c);
}
clang
S:\c>clang -Wempty-body test.c
test.c:8:10: warning: if statement has empty body [-Wempty-body]
if (c);
^
if
while
while
int main(void)
{
char c=1;
while (c);
{ --c; }
if (c);
}
test.c:6:10: warning: if statement has empty body [-Wempty-body]
if (c);
test.c:4:13: warning: while loop has empty body [-Wempty-body]
while (c);
gcc
while
clang
It's to avoid too many false positives. In general, if (expr);
never makes sense, but while (expr);
need not be an error, because expr
's side effects could cause the expression to switch from true to false. For instance,
void processAllElements() {
while (tryProcessNextElement());
}
Here's how the source explains it:
// `for(...);' and `while(...);' are popular idioms, so in order to keep
// noise level low, emit diagnostics only if for/while is followed by a
// CompoundStmt, e.g.:
// for (int i = 0; i < n; i++);
// {
// a(i);
// }
// or if for/while is followed by a statement with more indentation
// than for/while itself:
// for (int i = 0; i < n; i++);
// a(i);