<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/123153>123153</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang does not warn about missing returns when a switch statement covers all enum values
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jeremy-rifkin
</td>
</tr>
</table>
<pre>
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.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVE2P2zYQ_TX0ZbACRUqydNDB-TCQQ4GgDRD0SFEjiVmKdDmUHefXF6S86XZRoGgFw4LpN8N5896MIjKzQ-xZ_Y7VHw5qi4sP_TcMuN6fgpmejTsMfrz3XxaEyVvrb8bNoP2IEIOZZwwEClZDlM4Dxi04uKng0s_hDrPWTLyHla75rdwIOM4wbBGcj6CtcjOTJ8ZPrOH7R18ujJ_QbWv6mwg-Aju-A5XiB2DHD0y-S3h-Mi7C5D0T7UdAJrqEY_wEAEA3E_XCRPvmPD1aEcLHdKs8qf1yePU8SAi-3_NPUcMj6gGVr6CpPn56fL9QYvy0xHihFCbOTJxnPw7exsKHmYnzDybOQx1k5b5OzU4t9XtVxoEh2hAWDAiG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