This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/82924] New: gcc gives no warning for comparing unsigned integer < 0 even with -Wextra enabled


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82924

            Bug ID: 82924
           Summary: gcc gives no warning for comparing unsigned integer <
                    0 even with -Wextra enabled
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jure.slak at ijs dot si
  Target Milestone: ---

Created attachment 42568
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42568&action=edit
Compiling this file with -Wtype-limits should give a warning, but does not.

Take the following code

#include <iostream>

template<typename T>
T f(T x, unsigned y) {
    if (y < 0) return x;
    return static_cast<T>(0);
}

using namespace std;

int main() {

    int a = f(2, 3);
    std::cout << a << std::endl;

    return 0;
}

where function f clearly always returns 0. Compiling it with g++-7.2.0 -Wall
-Wextra gives no hint about pointless comparison. However, clang warns us
nicely:

a.cpp:7:11: warning: comparison of unsigned expression < 0 is always false
[-Wtautological-compare]
    if (y < 0) return x;
        ~ ^ ~
1 warning generated.

I believe that using -Wtype-limits (part of -Wextra) should warn about this
case, as it does in the non-template version.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]