On 2018-07-23 20:24:11 +0700, Robert Elz wrote: > Date: Mon, 23 Jul 2018 15:13:21 +0200 > From: Vincent Lefevre <vincent-o...@vinc17.net> > Message-ID: <20180723131321.gb12...@zira.vinc17.org> > > | No, this is not impossible. The result of the test is 0. > > Yes, I know, what I meant was that it is impossible for an > unsigned value to ever be < 0 - which is what that test is > based upon.
No, this is just a test. The expression is a valid C expression with a well-defined result, which may be 0 or 1. > But because of that, if one writes code like > > unsigned x,y,z; > > z = x - y; > if (z < 0) { /* whatever */ } > > the code is broken, This is valid C code. The "if (z < 0) { /* whatever */ }" is just dead code here. But this is not incorrect. Whether this is what the user intended to write is another matter. > and because of that, compilers warn about it GCC will warn just with -Wtype-limits, which isn't even in -Wall, thus not in general because such always-true or always-false tests are common with portability code: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=12963 But even if one gets a warning, this doesn't make the code wrong. Warnings are there just to signal a potential issue, sometimes with many false positives, like with this kind of tests. And my code was different: (T) -1 < 0 Here, (T) -1 is a constant expression. Thus GCC shouldn't warn on it, even with -Wtype-limits, as documented: -Wtype-limits Warn if a comparison is always true or always false due to the limited range of the data type, but do not warn for constant ^^^^^^^^^^^^^^^^^^^^^^^^ expressions. For example, warn if an unsigned variable is compared ^^^^^^^^^^^ against zero with "<" or ">=". This warning is also enabled by -Wextra. I get a warning, but this is a bug in the compiler. The goal concerning constant expressions is to avoid even more false positives. > (just like, in this meaningless example, they would also warn about > x and y being used without being set...) This other warning is useful because this is undefined behavior. -- Vincent Lefèvre <vinc...@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)