Skip to content

-Wswitch-default and -Wswitch-enum warning behaviour change request #148856

@r-barnes

Description

@r-barnes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions