[cfe-dev] Clang tautological-compare fails to warn in cases where gcc doesn't

Riyaz Puthiyapurayil via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 12 07:39:56 PDT 2018


Here is a simple test where clang fails to issue a warning in the first function but warns in the second function. gcc (6.x and above) gives a warning in both cases. Is the pointer dereference throwing clang off? Why should the pointer dereference matter in this case?


struct Foo {

    int a;

};



// Case 1

int cmp(const Foo* x, const Foo* y);

int cmp(const Foo* x, const Foo* y)

{

    if (x->a < x->a) {

        return -1;

    } else if (y->a > y->a) {

        return 1;

    }

    return 0;

}



// Case 2

int cmp2(int x, int y);

int cmp2(int x, int y)

{

    if (x < x) {

        return -1;

    } else if (y > y) {

        return 1;

    }

    return 0;

}



// gcc 6.2 with -Wall

x.cc: In function ?int cmp(const Foo*, const Foo*)?:

x.cc:7:14: warning: self-comparison always evaluates to false [-Wtautological-compare]

     if (x->a < x->a) {

         ~~~~~^~~~~~

x.cc:9:21: warning: self-comparison always evaluates to false [-Wtautological-compare]

     } else if (y->a > y->a) {

                ~~~~~^~~~~~

x.cc: In function ?int cmp2(int, int)?:

x.cc:17:11: warning: self-comparison always evaluates to false [-Wtautological-compare]

     if (x < x) {

         ~~^~~

x.cc:19:18: warning: self-comparison always evaluates to false [-Wtautological-compare]

     } else if (y > y) {

                ~~^~~



// clang 6.0.1 -Wall

x.cc:17:11: warning: self-comparison always evaluates to false [-Wtautological-compare]

    if (x < x) {

          ^

x.cc:19:18: warning: self-comparison always evaluates to false [-Wtautological-compare]

    } else if (y > y) {

                 ^

2 warnings generated.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180412/224417bd/attachment.html>


More information about the cfe-dev mailing list