Two case labels have the same value

tx0/c:duplicate-casedefaultwarningbug

What it finds

case 2: ... case 1 + 1:: two labels of one switch with the same value. A compiler rejects that, so in code that compiles it is a mistake made by a macro or found only in some configurations; in an editor it is found before compiling.

Values are worked out from integer and character literals, enumerators declared in the file and the arithmetic between them. An enumerator after a directive in its list, or one declared differently in other branches of a conditional directive, has no value here, and labels with a conditional directive between them are not compared, since they may never be compiled together.

From the rule's source, duplicate_case.rs.

Example from the rule's tests
enum color { RED, GREEN, BLUE = 4, CYAN }; enum flags {  F_A = 1 << 0,  F_B = 1 << 1,  F_C = F_A | F_B,}; int name(int c, int f, int ch){  switch (c) {  case RED:    return 0;  case GREEN:    return 1;  case 1:tx0 reports: the case 1 has the value 1 of an earlier case    return 2;  case CYAN:    return 3;  case 2 + 3:tx0 reports: the case 2 + 3 has the value 5 of an earlier case    return 4;  }  switch (f) {  case F_C:    return 5;  case 3:tx0 reports: the case 3 has the value 3 of an earlier case    return 6;  case F_A:    switch (ch) {    case 'a':      return 7;    case 1:      return 8;    }    return 9;  }  switch (ch) {  case 'A':    return 10;  case 65:tx0 reports: the case 65 has the value 65 of an earlier case    return 11;  case '\n':    return 12;  }  return -1;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/c:duplicate-case" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:duplicate-case] reason="..."

tx0 explain tx0/c:duplicate-case shows the same in the terminal. Found a false positive? Report it.