[PATCH] [clang-tidy] Add check for possibly incomplete switch statements

Alexander Kornienko alexfh at google.com
Tue Aug 5 16:30:37 PDT 2014


On Tue, Aug 5, 2014 at 7:37 PM, Benjamin Bannier <bbannier at gmail.com> wrote:

> Hi Alexander,
>
> > > While clang issues a warns about a possibly incomplete switch statement
> > > when switching over an enum variable and failing to cover all enum
> > > values (either explicitly or with a default case), no such warning is
> > > emitted if a plain integer variable is used as switch variable.
> > >
> >
> > If we consider this diagnostic useful, then we should implement
> > -Wswitch-default in Clang (GCC has it, Clang doesn't).
>
> This is intended to catch different problems than -Wswitch-default,


This check warns on "switching on non-enum value without default case".
GCC's -Wswitch-default warns "whenever a switch statement does not have a
default case". The difference is "non-enum value", which would make a
reasonable improvement over the GCC's behavior, as switching over enums is
better handled with -Wswitch. Once there's a compiler diagnostic in GCC
with a very close (but strictly wider) scope, it seems reasonable to
implement it in Clang also as a compiler diagnostic.


> and
> a default case is even discouraged in some style guides since it masks
> unhandled values when using -Wswitch (explicitly in GCC or implicitly in
> clang).
>
> The problem this tries to catch is switching on ints w/o an existing
> default. If the switch labels are enum values both GCC and clang will
> check for full coverage if one uses -Wswitch; however since the switch
> condition won't be of that enum type no full coverage can be guaranteed.
> The same problem also exists were the switch labels plain ints, but then
> even -Wswitch won't help.
>
> It might be a good idea to add a compiler warning proper for that, but I
> have even less experience with that than with writing AST matchers for
> clang-tidy.


This reason alone is not enough to decide in favor of a clang-tidy check in
this case. Implementing this specific diagnostic in the compiler shouldn't
be much harder. There are good examples which are quite close to what you
need - -Wswitch and -Wcovered-switch-default.


> Btw, I am working on comments in http://reviews.llvm.org/D4784.
>
>
> Cheers,
>
> Benjamin
>
> --
> "Good judgment comes from experience. Experience comes from bad judgment."
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140806/c095ba71/attachment.html>


More information about the cfe-commits mailing list