3 ms·
Clang and GCC have something like this but restricted to just comparisons, called -Wtautological-compare. I recommend turning it on for the same reasons. Althou
by kccqzy 3y ago
Clang and GCC have something like this but restricted to just comparisons, called -Wtautological-compare. I recommend turning it on for the same reasons. Although in practice it may need to be suppressed in macro-heavy or template-heavy code.
This has also caught many real world bugs in C such as https://github.com/aircrack-ng/rtl8812au/issues/308 https://github.com/aircrack-ng/rtl8812au/issues/308
- Quentincestino 3y agoWell considering the danger an UB can represent in C/C++ code you always should run gcc/clang with at least -Wall
- Too 3y agoC compilers are funny on this one, because instead of warning you about code that don’t do anything, they take advantage of it, in the form of UB optimizations. Like you say, macros often make this very hard to enforce in practice.
- sapiogram 3y agoNone of the examples in the article would cause UB in analogous C code. A C compiler could still optimize away the useless check itself, but that's true for every language.