[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CA+55aFwUY1-S9gDO5tP2A84ttSxXdj_nM5FyEQx+8G2=Opd6rg@mail.gmail.com>
Date: Wed, 27 May 2015 16:18:51 -0700
From: Linus Torvalds <torvalds@...ux-foundation.org>
To: "Kirill A. Shutemov" <kirill@...temov.name>
Cc: Jens Axboe <axboe@...nel.dk>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>
Subject: Re: block: new gcc-5.1 warnings..
On Wed, May 27, 2015 at 3:56 PM, Kirill A. Shutemov
<kirill@...temov.name> 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@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists