public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/53479] New: Control flow analysis too alarming with switch over an enum class
@ 2012-05-24 19:26 0xd34df00d at gmail dot com
  2012-05-24 19:59 ` [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: 0xd34df00d at gmail dot com @ 2012-05-24 19:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53479

             Bug #: 53479
           Summary: Control flow analysis too alarming with switch over an
                    enum class
    Classification: Unclassified
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: 0xd34df00d@gmail.com


Created attachment 27492
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27492
A very simple testcase

If a non-void function consists of a switch over a variable of an enum class,
and all possible values of the enum class are listed in the cases, and each
case ends with a return, gcc 4.6 and later still emits a "control reaches end
of non-void function" warning. gcc 4.5 and earlier don't exhibit such behavior
(as well as clang, if that matters). See the attached file for an example.

While this is a somewhat reasonable behavior for switches over a plain enum, I
doubt it is OK to emit this warning, at least, with such a generic option as
-Wreturn-type. If neither case succeeds, then you have had an UB somewhere in
your code previously, but I doubt it's good to warn the user in the switch.

Moreover, adding a 'default' clause allows to shoot yourself in the foot later
when adding one more field in the enum — the compiler won't warn you that you
didn't check it. Adding a dummy return after the switch seems more like a
kludge.

If there are strong considerations for this warning in this case, I suggest
moving it to a separate warning class, like -Wreturn-enum-class or something
like that. This way those who don't need this check will be able to disable it
without hurting other cases where it's useful for them.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches
  2012-05-24 19:26 [Bug c++/53479] New: Control flow analysis too alarming with switch over an enum class 0xd34df00d at gmail dot com
@ 2012-05-24 19:59 ` redi at gcc dot gnu.org
  2012-05-24 20:06 ` 0xd34df00d at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-05-24 19:59 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53479

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-05-24 19:57:19 UTC ---
  Foo f = Foo(2);
  assert( DoFoo( f ) );

Undefined behaviour.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches
  2012-05-24 19:26 [Bug c++/53479] New: Control flow analysis too alarming with switch over an enum class 0xd34df00d at gmail dot com
  2012-05-24 19:59 ` [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches redi at gcc dot gnu.org
@ 2012-05-24 20:06 ` 0xd34df00d at gmail dot com
  2012-05-24 20:30 ` redi at gcc dot gnu.org
  2012-10-10 19:11 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 5+ messages in thread
From: 0xd34df00d at gmail dot com @ 2012-05-24 20:06 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53479

--- Comment #2 from Georg Rudoy <0xd34df00d at gmail dot com> 2012-05-24 20:04:56 UTC ---
(In reply to comment #1)
>   Foo f = Foo(2);
>   assert( DoFoo( f ) );
> 
> Undefined behaviour.

Yes, it is. And isn't it happening at the point of cast of an integer to the
enum class type?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches
  2012-05-24 19:26 [Bug c++/53479] New: Control flow analysis too alarming with switch over an enum class 0xd34df00d at gmail dot com
  2012-05-24 19:59 ` [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches redi at gcc dot gnu.org
  2012-05-24 20:06 ` 0xd34df00d at gmail dot com
@ 2012-05-24 20:30 ` redi at gcc dot gnu.org
  2012-10-10 19:11 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-05-24 20:30 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53479

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-05-24 20:21:57 UTC ---
No, there's nothing wrong with the cast.

A scoped enumeration type without an explicitly-specified underlying type has a
fixed underlying type of int, so the values of the enumeration type are the
values of int.

Your switch doesn't handle all values, so control can flow off the end of the
function, so the warning is correct.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches
  2012-05-24 19:26 [Bug c++/53479] New: Control flow analysis too alarming with switch over an enum class 0xd34df00d at gmail dot com
                   ` (2 preceding siblings ...)
  2012-05-24 20:30 ` redi at gcc dot gnu.org
@ 2012-10-10 19:11 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 5+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-10-10 19:11 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53479

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #4 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-10-10 19:11:20 UTC ---
Closing.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-10-10 19:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-24 19:26 [Bug c++/53479] New: Control flow analysis too alarming with switch over an enum class 0xd34df00d at gmail dot com
2012-05-24 19:59 ` [Bug c++/53479] Control flow analysis reports warnings in switch over an enum class even if all possible values have their branches redi at gcc dot gnu.org
2012-05-24 20:06 ` 0xd34df00d at gmail dot com
2012-05-24 20:30 ` redi at gcc dot gnu.org
2012-10-10 19:11 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).