-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Fix all fall-through warnings in order to enable -Wimplicit-fallthrough for Clang.
In preparation to enable -Wimplicit-fallthrough for Clang, explicitly
add multiple break/goto/return/fallthrough statements instead of letting
the code fall through to the next case.
Notice that in order to enable -Wimplicit-fallthrough for Clang, this change[1]
is meant to be reverted at some point.
Something important to mention is that there is currently a discrepancy
between GCC and Clang when dealing with switch fall-through to empty case
statements or to cases that only contain a break/continue/return
statement[2][3][4].
Now that the -Wimplicit-fallthrough option has been globally enabled[5],
any compiler should really warn on missing either a fallthrough annotation
or any of the other case-terminating statements (break/continue/return/
goto) when falling through to the next case statement. Making exceptions
to this introduces variation in case handling which may continue to lead
to bugs, misunderstandings, and a general lack of robustness. The point
of enabling options like -Wimplicit-fallthrough is to prevent human error
and aid developers in spotting bugs before their code is even built/
submitted/committed, therefore eliminating classes of bugs. So, in order
to really accomplish this, we should, and can, move in the direction of
addressing any error-prone scenarios and get rid of the unintentional
fallthrough bug-class in the kernel, entirely, even if there is some minor
redundancy. Better to have explicit case-ending statements than continue to
have exceptions where one must guess as to the right result. The compiler
will eliminate any actual redundancy.
[1] commit e2079e9 ("kbuild: Do not enable -Wimplicit-fallthrough for clang for now")
[2] ClangBuiltLinux#636
[3] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91432
[4] https://godbolt.org/z/xgkvIh
[5] commit a035d55 ("Makefile: Globally enable fall-through warning")
The following patch (already in mainline 5.10-rc2 :) ) addresses almost 40,000 fall-through warnings:
include: jhash/signal: Fix fall-through warnings for Clang
linux$ git branch --show-current
linux-5.10-rc1
linux$ git log --oneline | head -2
0d3f38450734 Revert "kbuild: Do not enable -Wimplicit-fallthrough for clang for now"
3650b228f83a Linux 5.10-rc1
linux$ grep 'warning: unannotated fall-through' clang-ft-linux-5.10-rc1-allmodconfig.log | wc -l
39608
linux$
linux$ git checkout linux-5.10-rc2
Switched to branch 'linux-5.10-rc2'
linux$ git log --oneline | head -2
f792d8357774 Revert "kbuild: Do not enable -Wimplicit-fallthrough for clang for now"
3cea11cd5e3b Linux 5.10-rc2
linux$ grep 'warning: unannotated fall-through' clang-ft-linux-5.10-rc2-allmodconfig.log | wc -l
442
There are currently 440 of these warnings remaining in next-20201102:
linux$ git log --oneline | head -2
d7e66b50d324 Revert "kbuild: Do not enable -Wimplicit-fallthrough for clang for now"
b49976d8ef64 Add linux-next specific files for 20201102
linux$ grep 'warning: unannotated fall-through' clang-testing-next-20201102.log | wc -l
440