Skip to content

clang-diagnostics: -Wswitch-default and -Wcovered-switch-default contradictory warnings #82960

Closed as not planned
@FalcoGer

Description

@FalcoGer

When having both -Wswitch-default and -Wcovered-switch-default this generates contradictory warnings. When you don't have a default case for a switch statement that handles every enum value you get a warning for not having it; and if you do you get a warning for having it. However you require both those flags if you wish to get related warnings elsewhere when dealing with switch statements not related to enums or enums but not all the values.

Example

#include <cstdint>

enum class ETest : std::uint8_t
{
    TEST1,
    TEST2,
};

auto main() -> int
{
    ETest test = ETest::TEST2;
    [[maybe_unused]]
    int   a {};

    switch (test)
    {
        case ETest::TEST1: a = 1; break;
        case ETest::TEST2: a = 2; break;
        // default:           a = 99; break;
    }

    return 0;
}

What I would expect is that -Wswitch-default is suppressed when dealing with enums and every case is covered, but active otherwise.

The way it is now, you will miss out on at least one of those three possible cases that require a warning:

  • A switch statement that is not handling an enum doesn't have a default case (which is bad)
  • A switch statement that is dealing with an enum is fully defined but has a default case (which is bad)
  • A switch statement that is dealing with an enum is not fully defined and doesn't have a default case (which is bad)

This was not the case in earlier versions of clang, which handled this correctly.

My version is

Ubuntu clang version 19.0.0 (++20240222031214+307409a8872f-1~exp1~20240222151237.1514)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions