I’d like to have the compiler warn me if I’m not handling every if statement’s else condition. Does this exist in either clang or gcc?
To clarify, I’m not trying to have this be on for all of my source code. However, there are sometimes entire files or large swaths of code for which I simply cannot afford to not think hard about every single else block, by design. So, I suppose, I’m really looking for a pragma I can turn on and off to enable and disable this for a few thousands of lines of very important code.
Imagine it as an automated code review, or static analysis tool.
To say that the compiler can’t do it because it’s legal is … not a problem in practice. Every C/C++ compiler I’ve ever seen will gladly emit plenty of warnings against code that is perfectly syntactically and semantically valid. (For example in gcc, -Wunused-value, -Wunused-label, -Wunreachable-code, etc…)
If you really want this, I might suggest downloading Cppcheck and adding a check for this. Cppcheck does simple kinds of text-based matching to check against a rule set. It would be reasonably straightforward to warn for a missing
elseclause.I implemented a prototype check for “missing else” in the
missing-elsebranch here: https://github.com/ghewgill/cppcheck/tree/missing-else. This passes its own test but fails a lot of other tests because of the new unexpected style warning (on otherwise legitimate code).