Suppress -Wtautological comparison

I have code like

Q_ASSERT(value_which_is_always_smaller_than_4 < 4) 

where Q_ASSERT is the Qts assert macro. Now clang, seeing this, warns me about it, because the comparison is always true. It's nice that this can detect this, but this is the point of approval. Can I somehow suppress the warning, but only in the statements? I still liked being warned in other places.

+4
source share
2 answers

You can define a new macro to carry Q_ASSERT and automatically disable the warning with #pragma clang diagnostic ignored :

 #define STR(x) #x #define PRAGMA(x) _Pragma(STR(x)) #define MY_ASSERT(x) PRAGMA(clang diagnostic push) \ PRAGMA(clang diagnostic ignored "-Wtautological-compare") \ Q_ASSERT(x) \ PRAGMA(clang diagnostic pop) 

Now just do

 MY_ASSERT(3<4) 

should not cause a warning.

+10
source

You can disable it for the entire file by adding -Wno-tautological-compare to the Clang command line (after flags such as -Wall that include warnings). The disadvantage of this method is that the warning is now disabled throughout this translation unit, and not just for Q_ASSERT(...) macro Q_ASSERT(...) .

Another, more tedious but fine-grained method is to wrap each instance of the macro that generates this warning, as follows:

 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wtautological-compare" Q_ASSERT(value_which_is_always_smaller_than_4 < 4) #pragma clang diagnostic pop 
+2
source

All Articles