Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid false-positive -Wswitch warnings

Tags:

c

enums

Let's have a list of values (foo.lst):

foo,
bar,
baz,

Let's make an enum out of this

enum foo {
  #include "foo.lst"
  _foo_length
};

Let's use that enum in a switch:

int main(void) {
  enum foo[_foo_length];

  switch(f[0]) {
    case foo: return 0;
    case bar: return 0;
    case baz: return 0;
  }

  __builtin_unreachable();
}

(this code is dumb, but just ignore that)

The problem:
With -Wswitch (included in -Wall), GCC and Clang (and probably others) will warn:

warning: enumeration value '_foo_length' not handled in switch [-Wswitch]

Solutions:

  • Disabling -Wno-switch hides that warning.
    Downside: we lose warnings about any other case missing from the switch.
  • Adding a default: unreachable(); case.
    Downside: we lose compile-time warnings of missing cases, in favour of a runtime crash if we ever hit one of the missing cases when debugging.
  • Replacing the last value of the enum with a #define _foo_length (baz + 1), making it not part of the enum anymore.
    Downside: it requires that define to be manually updated every time a value is added to the list. Someone will invariably forget, breaking everything.

Ideally, there should be a way to mark an enum's value as no being assignable, thereby making it not produce a warning in the compiler when reading the possible values and this one isn't there, without requiring a preprocessor macro needing duplicate modifications.

Is there anything like that? Any other option I didn't think of?

like image 853
1ace Avatar asked Aug 26 '16 16:08

1ace


People also ask

How do you prevent false positive AML?

To distinguish correct AML alerts, the data processed should be as simple and straightforward as possible. Therefore, companies that organize data based on specific customer information rather than under a single heading will greatly reduce false positives.

What are some steps scientists can take to avoid false negatives?

The best way to avoid a false negative result, is to ensure that your experiment has sufficient power before you conduct it. A power analysis will help you determine how many observations (i.e. users passing through your test) are needed in order to reliably detect a given amount of difference.


1 Answers

Use

case _foo_length:
    unreachable();
    break;

so that all the cases will be handled. You could create a macro for it to keep it from being so verbose.

like image 163
Barmar Avatar answered Nov 15 '22 12:11

Barmar