Re: block: new gcc-5.1 warnings..

From: Linus Torvalds
Date: Wed May 27 2015 - 19:18:58 EST


On Wed, May 27, 2015 at 3:56 PM, Kirill A. Shutemov
<kirill@xxxxxxxxxxxxx> wrote:
>
> In which situation fall-through switch() would make better structure then
> plain if()? It's easier to miss-read fall-through.

"switch()" is rather more expressive than "if ()", mostly thanks to "break".

With switch(), you can write things like

switch (a) {
case 1:
A;
if (b)
break;
B;
/* fallthrough */
case 2:
C;
}

where you share that "C" case for some conditions but not others.

You can do the same using goto, of course, but you can *not* do it
with pure nested "if ()" statements.

So even with just two cases, "switch ()" is syntactically more
powerful than "if ()" because it allows more structured exits.

So anybody who says that "booleans don't make sense for switch()" is just crazy.

Now, the reason the warning actually happened to make sense for the
kernel wasn't because it was a boolean, but because the type of the
switch and the case statements didn't match.

So I do actually agree that

switch (boolean) {
case non-boolean:

can very much be worth a warning. But then it's about type-safety
issues, rather than about "you shouldn't use switch() with a boolean".

See the difference?

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/