On Thu, Jun 26, 2014 at 04:32:06PM +0000, Polehn, Mike A wrote:
> There is a very good reason for putting constants on the left hand side of a 
> compare statement.
> For example:
> if (NULL = x)  
> will be a compiler error, while the following will compile and need debugging:
> if (x = NULL)
> 
> Although I try not making the comparison mistakes, I have recently made that 
> exact mistake and had to debug.
> If I had used the second format, the complier would have output an error and 
> saved the time of debugging.

I'm quite aware.

Every decent compiler already issues a diagnostic.  Clang:

../lib/dynamic-string.c:304:16: error: using the result of an assignment as a
      condition without parentheses [-Werror,-Wparentheses]
        if (*s = '#') {
            ~~~^~~~~
../lib/dynamic-string.c:304:16: note: place parentheses around the assignment to
      silence this warning
        if (*s = '#') {
               ^
            (       )
../lib/dynamic-string.c:304:16: note: use '==' to turn this assignment into an
      equality comparison
        if (*s = '#') {
               ^
               ==

GCC:

../lib/dynamic-string.c: In function 'ds_get_test_line':
../lib/dynamic-string.c:304:9: error: suggest parentheses around assignment 
used as truth value [-Werror=parentheses]

sparse:

../lib/dynamic-string.c:304:16: warning: assignment expression in conditional
_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to