Skip to content

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

@jeremy-rifkin

Description

@jeremy-rifkin

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 #122382 and #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.

Metadata

Metadata

Assignees

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