Skip to content

-Wenum-compare doesn't catch mismatched enum constant comparison in C mode #29217

@llvmbot

Description

@llvmbot
Bugzilla Link 28847
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @mmdriley

Extended Description

Consider this program (test.c)

enum foo_t {
    foo1, foo2, foo3
};

enum bar_t {
    bar1, bar2, bar3
};

int main(void) {
    enum foo_t foo = foo1;
    enum bar_t bar = bar1;

    int cmp1 = foo < bar; // variable - variable
    int cmp2 = foo < bar2; // variable - constant
    int cmp3 = foo1 < bar3; // constant - constant

    return cmp1 || cmp2 || cmp3;
}

If I compile this as C code, I only get a warning for the first comparison. If I compile in C++ mode, I get a warning for all three.

$ clang -Wall test.c
test.c:13:20: warning: comparison of two values with different enumeration types
      ('enum foo_t' and 'enum bar_t') [-Wenum-compare]
    int cmp1 = foo < bar;
               ~~~ ^ ~~~
1 warning generated.
$ clang -Wall -x c++ test.c
test.c:13:20: warning: comparison of two values with different enumeration types
      ('enum foo_t' and 'enum bar_t') [-Wenum-compare]
    int cmp1 = foo < bar;
               ~~~ ^ ~~~
test.c:14:20: warning: comparison of two values with different enumeration types
      ('enum foo_t' and 'bar_t') [-Wenum-compare]
    int cmp2 = foo < bar2;
               ~~~ ^ ~~~~
test.c:15:21: warning: comparison of two values with different enumeration types ('foo_t' and 'bar_t')
      [-Wenum-compare]
    int cmp3 = foo1 < bar3;
               ~~~~ ^ ~~~~
3 warnings generated.

I think the warnings should be emitted in C mode too. Tested with gcc 4.9.2 and 3 warnings are emitted for C code:

$ gcc -Wall test.c
test.c: In function 'main':
test.c:13:20: warning: comparison between 'enum foo_t' and 'enum bar_t' [-Wenum-compare]
     int cmp1 = foo < bar;
                    ^
test.c:14:20: warning: comparison between 'enum foo_t' and 'enum bar_t' [-Wenum-compare]
     int cmp2 = foo < bar2;
                    ^
test.c:15:21: warning: comparison between 'enum foo_t' and 'enum bar_t' [-Wenum-compare]
     int cmp3 = foo1 < bar3;
                     ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillacclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-negativeWarning doesn't fire when it should

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions