Why you might sometimes need to add -Wunreachable-code for C code
Published 09 February 2012 under C, gcc
Often when you have unreachable code it means that you've not coded what you thought you had, e.g. curly braces are in the wrong place.
By default, compilers such as gcc and clang (llvm) will compile the following code without warnings, even with -Wall set. In the original this silly example came from, the curly brace after "return 10", should have been after the "return 3".
int dodgyfn(int a)
{
if (a > 0)
{
return 3;
if (a > 5)
return 10;
}
return 8;
}
int main(void)
{
return dodgyfn(5);
}
If you recompile this code with -Wunreachable-code then you should get a warning about "code will never be executed". For me, I do get the warning with clang, but not with gcc (4.6.1, it seems to have been already reported as a gcc bug).
So, if you're a user of C it may be worth running clang or gcc (pre 4.6) now and then to look for such problems. This probably can't be added to a build system as built-in libraries etc. may produce many such warnings.
Comments
blog comments powered by Disqus