llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang does not warn about missing returns when a switch statement covers all enum values

Open jeremy-rifkin opened this issue 1 year ago • 6 comments

The following code triggers a missing return warning by gcc, msvc, and edg but not clang:

enum class E { a, b };

int foo(E e) {
    switch(e) {
        case E::a:
            return 20;
        case E::b:
            return 30;
    }
}

https://godbolt.org/z/b5r34nWf6

The main issue here is safety: This code is bug-prone due to extraneous enum values. The most common situation this could lead to bugs is for flag enums, e.g. https://godbolt.org/z/zx6nnPYMq.

Secondarily: This can lead to some annoyances where code compiles fine on clang but has a flood of warnings on gcc (possibly fatal ones, if using -Werror=return-type). This leads to PRs such as https://github.com/llvm/llvm-project/pull/122382 and https://github.com/llvm/llvm-project/pull/105520.

I'd like to propose changing the behavior to warn on this case by default. I briefly discussed this with folks on the llvm discord as well: https://discord.com/channels/636084430946959380/636732781086638081/1329124923322531850.

jeremy-rifkin avatar Jan 16 '25 04:01 jeremy-rifkin

@llvm/issue-subscribers-clang-frontend

Author: Jeremy Rifkin (jeremy-rifkin)

The following code triggers a missing return warning by gcc, msvc, and edg but not clang: ```cpp enum class E { a, b };

int foo(E e) { switch(e) { case E::a: return 20; case E::b: return 30; } }

https://godbolt.org/z/b5r34nWf6

The main issue here is safety: This code is bug-prone due to extraneous enum values. The most common situation this could lead to bugs is for flag enums, e.g. https://godbolt.org/z/zx6nnPYMq.

Secondarily: This can lead to some annoyances where code compiles fine on clang but has a flood of warnings on gcc (possibly fatal ones, if using -Werror=return-type). This leads to PRs such as https://github.com/llvm/llvm-project/pull/122382 and https://github.com/llvm/llvm-project/pull/105520.

I'd like to propose changing the behavior to warn on this case by default. I briefly discussed this with folks on the llvm discord as well: https://discord.com/channels/636084430946959380/636732781086638081/1329124923322531850.
</details>

llvmbot avatar Jan 16 '25 04:01 llvmbot

Hello, we're interested in this for Chromium V8 as well. Would making this diagnostics an opt-in behavior (guarded by a default-off flag) be considered a reasonable path forward without breaking compatibility with old codebases? Draft: #198041.

emaxx-google avatar May 16 '26 12:05 emaxx-google

I would also be interested on such improvement for safety critical systems. What would be the plan for cases where the default is there but a label is not explicit? Would this be covered by this issue or should I create another? Example: https://godbolt.org/z/WoqjYhTYK In such cases in might be a bug or it might be that the user really wanted to end up in the default. In safety critical system we would go in the direction on asking being explicit. I would also agree that should be off by default, the most common case where people might not want such annoyance is when the last element of the labeled enum is something like COUNT and you most probably do not want to have to put it always together with the default.

limdor avatar May 19 '26 12:05 limdor

@limdor

I would also be interested on such improvement for safety critical systems. What would be the plan for cases where the default is there but a label is not explicit? Would this be covered by this issue or should I create another? Example: https://godbolt.org/z/WoqjYhTYK In such cases in might be a bug or it might be that the user really wanted to end up in the default. In safety critical system we would go in the direction on asking being explicit. I would also agree that should be off by default, the most common case where people might not want such annoyance is when the last element of the labeled enum is something like COUNT and you most probably do not want to have to put it always together with the default.

What you're describing sounds like the existing -Wswitch-enum.

emaxx-google avatar May 19 '26 13:05 emaxx-google

@limdor

I would also be interested on such improvement for safety critical systems. What would be the plan for cases where the default is there but a label is not explicit? Would this be covered by this issue or should I create another? Example: https://godbolt.org/z/WoqjYhTYK In such cases in might be a bug or it might be that the user really wanted to end up in the default. In safety critical system we would go in the direction on asking being explicit. I would also agree that should be off by default, the most common case where people might not want such annoyance is when the last element of the labeled enum is something like COUNT and you most probably do not want to have to put it always together with the default.

What you're describing sounds like the existing -Wswitch-enum.

Sorry you are right, I will have to try with my original example again because I thought I tried and it was not being triggered, but in the example I posted from godbolt in the moment I add -Wswitch-enum it triggers a warning.

Sorry for the confusion

limdor avatar May 19 '26 14:05 limdor

The main issue here is safety: This code is bug-prone due to extraneous enum values. The most common situation this could lead to bugs is for flag enums, e.g. https://godbolt.org/z/zx6nnPYMq.

Remark that historically, for the -Wswitch warning, clang has made different assumptions, and required that you mark flag enums explicitly, with the flag_enum attribute: https://reviews.llvm.org/D5853 (landed as https://github.com/llvm/llvm-project/commit/724f14e75c50c6839408509b2fede5464fa9111e): "This patch introduces a new attribute, flag_enum, which indicates to the compiler that an enumerator is a flag type. Compiler warnings about the range of values are adjusted accordingly." This attribute was also adopted in gcc-15: https://developers.redhat.com/articles/2025/04/24/new-c-features-gcc-15

It would be desirable that clang has some universal view on when an enum (or enum class) can hold non-declared values.

brunodf-gf avatar Aug 20 '26 13:08 brunodf-gf