Consider the following enumeration declaration: enum status { ok = 0, err = -1, foo, bar }; The enumerators 'ok' and 'foo' will have the same value. This may be what the programmer intended, but it's more likely to be an oversight. The C and C++ front ends should have a warning (which probably should not be enabled by default or with -Wall) for enumerations whose enumerators have duplicate values, since duplicates are likely to be a sign of programmer error.
Confirmed.
*** Bug 107377 has been marked as a duplicate of this bug. ***
FWIW, Clang implements this warning: $ xclang++ -c -xc j.c -Wduplicate-enum j.c:1:33: warning: element 'foo' has been implicitly assigned 0 which another element has been assigned [-Wduplicate-enum] enum status { ok = 0, err = -1, foo, bar }; ^~~ j.c:1:15: note: element 'ok' also has value 0 enum status { ok = 0, err = -1, foo, bar }; ^~~~~~ I suppose we could do a binary search when adding a new enumerator in build_enumerator, see if an enumerator with the same value is already present, and warn if so.
(In reply to Marek Polacek from comment #3) > FWIW, Clang implements this warning: Oh, I was unable to find the option (only tried -Werror and -Wextra). @Marek: please add it!
(In reply to Martin Liška from comment #4) > @Marek: please add it! Anything to oblige ye, my fellow.
(In reply to Marek Polacek from comment #5) > (In reply to Martin Liška from comment #4) > > @Marek: please add it! > > Anything to oblige ye, my fellow. It would pay off my friend (PR107386).
Should be way to disable/enable this warning per `enum`? Like: ``` enum [[gnu::enum_unique]] X { I = 1, J = 2, }; enum [[gnu::enum_not_unique]] Y { K = 1, L = 1, }; ```
(In reply to trashyankes from comment #7) > Should be way to disable/enable this warning per `enum`? > Like: > > ``` > enum [[gnu::enum_unique]] X > { > I = 1, > J = 2, > }; > > enum [[gnu::enum_not_unique]] Y > { > K = 1, > L = 1, > }; > > ``` No, it should warn solely if enumerator gets implicitly assigned value of some other enumerator. After all, the clang wording also mentions that: element 'D' has been implicitly assigned 5 which another element has been assigned [-Wduplicate-enum] If there is = value for it, we should never warn, that is most likely completely intentional. Of course, we should use better wording, because we should talk about enumerators, not elements...