Hi,
[Adding helenos-devel back in the loop].
if the argument of the switch that has no default branch is of an enum type,
GCC will emit a warning if all the enum members are not handled. This way,
whenever you add a new member to the enum, you will get a warning (actually
an error, due to -Werror) on every such switch statement and will have to go
through them and to add proper handling for the new case. Wonderful!
I've been using this approach systematically for the last five years or so
and it has proven extremely useful. If you pay proper attention to my switch
statements, you will notice it ^_^. There are some things you need to watch
for, though:
- since technically the switch expression can have a value that is not one
of the enum members, GCC will complain if you work with the assumption that
it cannot happen (for example you have a variable that you initialize only
in each switch case - then it will *correctly* warn you that the variable
can be uninitialized). You need to structure the code so that it is formally
correct even if another value is given for the switch expression
- Clang static analyzer, OTOH *will* assume that the switch expression
cannot have an 'invalid' value and might complain if you try to handle that
case ... sometimes it's difficult to satisfy everybody :-(
Maybe this would be worth documenting in the wiki because it's a coding
technique that's used (at least by me ^_^) and clearly it's not immediately
obvious.
Cheers,
Jiri
---------- Původní e-mail ----------
Od: Jiří Zárevúcky <[email protected]>
Komu: Jiri Svoboda <[email protected]>
Datum: 5. 10. 2017 15:51:58
Předmět: Re: [HelenOS-commits] [mainline] r2779 - There is a good reason why
there should be no default branch.
"Hmm, what is the reason?
To me, it looks functionally identical.
On 5 October 2017 at 12:09, <[email protected]> wrote:
> Repo: mainline
> Committer: Jiri Svoboda <jiri@wiwaxia>
> Date: 2017-10-05 09:07:45 CEST (Thu, 05 Oct 2017)
> New Revision: 2779
> New Id: jiri@wiwaxia-20171005070745-zfcdr462pwy038t8
> Parent: jiri@wiwaxia-20171004221410-wvyre0qs1jv446ct
>
> Log:
> There is a good reason why there should be no default branch.
>
> Modified:
> uspace/lib/c/include/types/label.h
> uspace/lib/fdisk/src/fdisk.c
>
> === modified file 'uspace/lib/c/include/types/label.h'
> --- uspace/lib/c/include/types/label.h 2015-10-20 10:08:15 +0000
> +++ uspace/lib/c/include/types/label.h 2017-10-05 07:07:45 +0000
> @@ -123,6 +123,8 @@
> lpc_minix
> } label_pcnt_t;
>
> +#define LPC_LIMIT (lpc_minix + 1)
> +
> #endif
>
> /** @}
>
> === modified file 'uspace/lib/fdisk/src/fdisk.c'
> --- uspace/lib/fdisk/src/fdisk.c 2017-10-04 18:22:12 +0000
> +++ uspace/lib/fdisk/src/fdisk.c 2017-10-05 07:07:45 +0000
> @@ -1014,6 +1014,8 @@
> min_blocks = fdisk_ba_align_up(dev, min_blocks);
> max_blocks = fdisk_ba_align_up(dev, max_blocks);
>
> + pcnt = LPC_LIMIT;
> +
> switch (pspec->fstype) {
> case fs_exfat:
> pcnt = lpc_exfat;
> @@ -1029,9 +1031,10 @@
> break;
> case fs_cdfs:
> return EINVAL; /* You cannot create an ISO partition */
> - default:
> + }
> +
> + if (pcnt == LPC_LIMIT)
> return EINVAL;
> - }
>
> if (pspec->pkind == lpk_logical) {
> hdrb = max(1, dev->align);
>
>
"_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/listinfo/helenos-devel