Bug 91432 - gcc -Wimplicit-fallthrough does not warn when fallthrough to break;
Summary: gcc -Wimplicit-fallthrough does not warn when fallthrough to break;
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-13 07:40 UTC by Joe Perches
Modified: 2021-07-27 22:32 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joe Perches 2019-08-13 07:40:57 UTC
This code does not emit a fallthrough warning:

int foo(int i)
{
  switch (i) {
  case 1:
    i = 0;
  default:
    break;
  }
  return i;
}

Basically any case block that falls through to another
block of just a break statement does not get a warning.

Is this a defect or what was the logic behind this decision?
Comment 1 Marc Glisse 2019-08-13 08:16:07 UTC
The warning basically says "you may have forgotten 'break;'". If it falls through to break anyway, what difference does it make if I add a redundant break?
Comment 2 Martin Liška 2020-01-27 13:56:39 UTC
I would close this as invalid.
Comment 3 Nick Desaulniers 2020-06-04 17:45:25 UTC
Isn't this still an implicit fallthrough, though?  It may not "be a bug," but that's why warnings are not errors; they may not result in "bugs."

The issue is that Clang will warn in this case, so developers get confused about writing compiler-portable code with intentional fallthrough vs being warned about unintentional fallthrough.

Why shouldn't developers annotate that the implicit fallthrough from `case 1` to `default` was intentional, making it explicit?
Comment 4 Jakub Jelinek 2021-07-22 18:25:30 UTC
The warning has been carefully designed not to warn in common cases which pose no problems, see e.g. https://developers.redhat.com/blog/2017/03/10/wimplicit-fallthrough-in-gcc-7 or the lengthy discussions held around the submission of the feature. Not warning in this case is a very intentional part of those design decisions.
Comment 5 Nick Desaulniers 2021-07-27 20:20:18 UTC
> Not warning in this case is a very intentional part of those design decisions.

Can you provide a link to the discussion about this specific case?

Is re-evaluating the decision out of the question?
Comment 6 Jakub Jelinek 2021-07-27 20:34:52 UTC
(In reply to Nick Desaulniers from comment #5)
> > Not warning in this case is a very intentional part of those design decisions.
> 
> Can you provide a link to the discussion about this specific case?

The discussions are on gcc-patches mailing list, look into archives from July 2016 to August or September, initially with -Wswitch-fallthrough in the subject, later -Wimplicit-fallthrough.

> Is re-evaluating the decision out of the question?

Yes.  It is pointless to start warning about code that obviously can't do any harm, it will only alienate users that will need to mark up more code.
The work Marek has done on this warning started with getting through warnings on gcc itself and on other projects and has been fine tuned on where it is essential not to warn and where false positives can be acceptable.
Comment 7 Joe Perches 2021-07-27 22:28:33 UTC
What could be useful is to add yet another --extra-strict-fallthrough warning
flag that would make it possible for these cases to have a warning.
Comment 8 Jakub Jelinek 2021-07-27 22:32:24 UTC
Useful for what?  What exactly is an advantage to require attribute at such a place?  Nothing will warn if you put it there, but I don't see a rationale for requiring it.