-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Description
Goals
We have a large codebase with many switches. -Wimplicit-fallthrough
, -Wunreachable-code-break
, and -Wunreachable-code-fallthrough
have been invaluable for improving the safety of this code pattern.
We're thinking about switch cases now and the existing set of flags is proving unwieldy, as described below. Modifications to the behaviour of these flags would allow us to improve safety where it's needed most while leaving otherwise functional code unaltered.
Existing behaviours
-Wswitch-default
requires that all switches have a default, even if they cover all of their enum values. Satisfying this flag in a large project may require adding many default: std::unreachable()
or similar to the codebase.
-Wswitch-enum
requires that all enum switches list every enum value, even if they have a default. This, again, creates a lot of code noise.
Desired behaviours
-Wswitch-default
I think it would be better if -Wswitch-default
triggered only in situations where not all the switch cases are covered. The new warning message would be:
warning: ‘switch’ doesn't explicitly handle all cases and is missing a ‘default’ label
A new flag -Wswitch-default-strict
would cover the old behaviour: explicitly requiring default
everywhere.
-Wswitch-enum
-Wswitch-enum
should interpret default
as covering enum all enum cases that aren't otherwise handled. -Wswitch-enum-strict
would capture the old behaviour and require that all cases be listed explicitly, even if a default is present.