[cfe-users] -Wtautological-constant-out-of-range-compare false positive?

Fred fredm at spamcop.net
Fri Oct 25 21:46:04 PDT 2013


Hi,
I have just started using the new Apple branch of clang[1] and encountered -Wtautological-constant-out-of-range-compare for the first time with the following code:
#include <stdio.h>

typedef enum o {
    O_1 = 0,
    O_2,
} o;

int main(int argc, char** argv) {
  enum o e1 = 2;

  if( e1 >= 2 ) {
    printf( "That's not right\n" );
  }

  return 0;
}

$ clang -Wall t1.c 
t1.c:11:10: warning: comparison of constant 2 with expression of type 'enum o'
      is always false [-Wtautological-constant-out-of-range-compare]
  if( e1 >= 2 ) {
      ~~ ^  ~
1 warning generated.

I would have expected the e1 >= 2 expression to be fine as enums are treated as integers - just like the out of range allocation e1 = 2 did not prompt a warning.

Am I missing something here or is the warning throwing up a false positive?

Thanks,
Fred

[1]
$ clang --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix



More information about the cfe-users mailing list