Bug 16186 - gcc should have an option to warn about enumerations with duplicate values
Summary: gcc should have an option to warn about enumerations with duplicate values
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.0
: P3 enhancement
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: diagnostic
: 107377 (view as bug list)
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2004-06-24 22:39 UTC by Matt Austern
Modified: 2023-03-13 13:44 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-09-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Austern 2004-06-24 22:39:40 UTC
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.
Comment 1 Andrew Pinski 2004-06-29 02:47:43 UTC
Confirmed.
Comment 2 Andrew Pinski 2022-10-24 15:25:59 UTC
*** Bug 107377 has been marked as a duplicate of this bug. ***
Comment 3 Marek Polacek 2022-10-24 18:37:05 UTC
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.
Comment 4 Martin Liška 2022-10-24 18:43:13 UTC
(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!
Comment 5 Marek Polacek 2022-10-24 18:48:20 UTC
(In reply to Martin Liška from comment #4)
> @Marek: please add it!

Anything to oblige ye, my fellow.
Comment 6 Martin Liška 2022-10-24 19:18:44 UTC
(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).
Comment 7 trashyankes 2023-03-10 20:12:40 UTC
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,
};

```
Comment 8 Jakub Jelinek 2023-03-13 13:44:53 UTC
(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...