Why didn't the compiler warn me about an empty if-statement?
Solution 1
It's not an error because an empty statement is a valid statement; however, since it's certainly suspicious code it's the perfect candidate for a compiler warning - and in fact gcc -Wall -Wextra
does warn about this:
int foo(int x) {
if(x); {
return 42;
}
return 64;
}
/tmp/gcc-explorer-compiler116427-37-l1vpg4/example.cpp: In function 'int foo(int)':
2 : warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if(x); {
^
both clang
and VC++ do it too.
gcc 6 is even smarter (well, maybe too much), and takes even the indentation as a hint that something is wrong:
/tmp/gcc-explorer-compiler116427-76-1sfy0y/example.cpp: In function 'int foo(int)':
2 : warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if(x); {
^
2 : warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(x); {
^~
2 : note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
if(x); {
^
So, either you don't have the warnings cranked up enough, or your compiler isn't smart enough.
If you don't have the possibility to switch to a more helpful compiler, consider using static analysis tools; for example, in this case cppcheck
spots the error (when given the --enable=all --inconclusive
flags):
[mitalia@mitalia ~/scratch]$ cppcheck --enable=all --inconclusive emptyif.c
Checking emptyif.c...
[emptyif.c:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if' statement.
[emptyif.c:1]: (style) The function 'foo' is never used.
Addendum - relevant warnings for various compilers (feel free to update)
to recap, the relevant warning options are:
-
gcc
-Wempty-body
; included in-Wextra
; -
gcc>=6.0, also
-Wmisleading-indentation
can help; included in-Wall
; -
clang
-Wempty-body
; included in-Wextra
too; -
Visual C++ C4390, included in
/W3
Static analysis tools:
-
cppcheck
--enable=warning --inconclusive
; included in--enable=all --inconclusive
Solution 2
As Matteo's answer indicated, the code is absolutely valid. It's being interpreted this way:
if(condition)
; // do nothing
// unrelated block
{
// do something
}
It's a bit of a technicality, but conditions with empty bodies do have some very nice uses.
Lint and other such code sanity tools will warn about the unexpected change in indentation, and catch additional errors that may be stylistic though not technically compiler errors.
Or security problems, variable tainting, buffer management, potential maintenance problems like bad casts, etc. There are an awful lot of code problems that don't fall into the category of "compiler errors".
As @jpmc26 mentioned, this approach may be better since you don't have to switch compilers to use it. Though I also personally find value in the ability to run the two independently.
Related videos on Youtube
Comments
-
AlphaGoku over 1 year
I'm using Keil uVision v4.74 and have enabled the option "All Warnings".
I wrote the following intentional code:
if(condition matched) { //do something }
When I rebuilt my project, I got 0 errors, 0 warnings.
However, when I accidentally wrote:
if(condition matched); { //do something }
I also got 0 errors, 0 warnings.
It was next to impossible for me to find out that a small
;
following the if condition was the root of the problem.Why didn't the compiler treat it as a warning and inform me?
-
ckruczek almost 8 yearsWhy should he? Thats a perfectly well formed and legal statement. Nothing warnable here.
-
AlphaGoku almost 8 yearsWhen i declare a variable and dont use it, he say "variable declared but undefined". Should he say something like "if statement used but no body for if statement?"
-
someone almost 8 yearsOne thing I want to mention here. This is not at all related to
keil
.. in general programming also it will happen. -
Ian Ringrose almost 8 yearsThe compiler should output one and only one warning, “You are using c, don’t expect a safety net”.
-
Matteo Italia almost 8 years@IanRingrose: on the contrary, given that the runtime is completely without safety nets for performance reasons, having all the possible static analysis at compile time is extremely desirable.
-
GrandOpener almost 8 years@ckruczek But... that's exactly what warnings are for. If it weren't well formed and legal, it would be an error. Warnings are for things that are legal, but suspicious as likely logic errors.
-
-
AlphaGoku almost 8 yearsHow much more can you crank Keil up beyond "All Warnings"?
-
Matteo Italia almost 8 years@Vector9: I don't know, I never used Keil. Besides, it seems like Keil itself is mostly an IDE, what compiler backend does it use? Unfortunately "minor" compilers for embedded platforms often lag way behind "mainstream" compilers in this kind of diagnostics.
-
AlphaGoku almost 8 yearsMy keil uses Armcc.exe v5.03.0.76
-
Matteo Italia almost 8 years@Vector9: giving a quick look to the list of warnings supported by armcc there doesn't seem to be anything related to this issue. However, I see that Keil does support using
gcc
, so you may give it a try (maybe not in production, but just from time to time to have a look at a different set of compiler diagnostics). But again, I'm no Keil/armgcc expert, so I don't know if can be used mostly as a drop-in replacement or if it is going to be a nightmare to switch between compilers. -
Cody Gray almost 8 yearsYup, MSVC warns:
warning C4390: ';': empty controlled statement found; is this the intent?
. It's actually a level 3 warning, so you don't even need /W4. The default warning level will catch this one. -
mucaho almost 8 yearsIs there an analogue flag for
clang
that would produce this warning? -
Matteo Italia almost 8 years@mucaho:
-Wextra
includes it even in clang; the specific flag for this warning is the same as forgcc
, i.e.-Wempty-body
. -
davenpcj almost 8 yearsLint will warn about the unexpected change in indentation as well.
-
jpmc26 almost 8 years@davenpcj Suggesting the use of a linter may be worth posting as an answer, since it (presumably) wouldn't require switching compilers.