public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100700] New: -Wreturn-type has many false positives
@ 2021-05-20 10:33 gonzalo.gadeschi at gmail dot com
  2021-05-20 17:58 ` [Bug c++/100700] " msebor at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: gonzalo.gadeschi at gmail dot com @ 2021-05-20 10:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

            Bug ID: 100700
           Summary: -Wreturn-type has many false positives
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gonzalo.gadeschi at gmail dot com
  Target Milestone: ---

Example (https://clang.godbolt.org/z/o85KbdG7h):

int f(unsigned i) {
  if (i < unsigned(-1)) return i;
}

enum E { A, B };

int h(E e) {
  switch (e) {
  case A: return 0;
  case B: return 0;
  }
}

compiled with g++ (no -Wall, no -Wextra) warns with:

 warning: control reaches end of non-void function [-Wreturn-type]

for "f" and "h".

Notice that clang, which implements the same warning, does not warn for "h"
(https://clang.godbolt.org/z/Ev9eqsqPf).

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
@ 2021-05-20 17:58 ` msebor at gcc dot gnu.org
  2021-05-20 19:09 ` gonzalo.gadeschi at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-05-20 17:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
           Severity|normal                      |enhancement

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
[Please include the full command line and the full compiler output in bug
reports (as requested in https://gcc.gnu.org/bugs/#need). Links to third party
sites are not a substitute (they can go away and the compiler versions they use
can change).]

$ cat pr100700.c && gcc -S -xc++ pr100700.c

int f(unsigned i) {
  if (i < unsigned(-1)) return i;
}

enum E { A, B };

int h (enum E e) {
  switch (e) {
  case A: return 0;
  case B: return 0;
  }
}
pr100700.c: In function ‘int f(unsigned int)’:
pr100700.c:4:1: warning: control reaches end of non-void function
[-Wreturn-type]
    4 | }
      | ^
pr100700.c: In function ‘int h(E)’:
pr100700.c:13:1: warning: control reaches end of non-void function
[-Wreturn-type]
   13 | }
      | ^

In both examples the warning is strictly correct: in a call to f(-1) the
function falls off the end, and ditto in a call to h ((enum E)2).  I could see
value in relaxing the warning for the enum case and issuing it only under some
stricter setting (say with -Wextra or under a new level), similarly to
-Wswitch-default.  I think there already is a request along these lines.

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
  2021-05-20 17:58 ` [Bug c++/100700] " msebor at gcc dot gnu.org
@ 2021-05-20 19:09 ` gonzalo.gadeschi at gmail dot com
  2021-05-20 20:20 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gonzalo.gadeschi at gmail dot com @ 2021-05-20 19:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

--- Comment #2 from gnzlbg <gonzalo.gadeschi at gmail dot com> ---
> in a call to f(-1) the function falls off the end,

Indeed, thanks. Using <= in the condition removes the warning.

> and ditto in a call to h ((enum E)2)

Until C++17, creating an enum value that's out-of-range of the enum was
unspecified behavior. In C++ standard >= 17 (e.g. -std=c++17), this became
undefined behavior. 

So the warning should point at the place where this happens: (enum E)2. 

The only way in which "h(E e)" can fall of its end in C++>=17 is if the program
already had undefined behavior.

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
  2021-05-20 17:58 ` [Bug c++/100700] " msebor at gcc dot gnu.org
  2021-05-20 19:09 ` gonzalo.gadeschi at gmail dot com
@ 2021-05-20 20:20 ` redi at gcc dot gnu.org
  2021-05-20 21:14 ` gonzalo.gadeschi at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-20 20:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to gnzlbg from comment #2)
> Until C++17, creating an enum value that's out-of-range of the enum was
> unspecified behavior. In C++ standard >= 17 (e.g. -std=c++17), this became
> undefined behavior. 

With GCC it depends on the -fstrict-enums option.

> The only way in which "h(E e)" can fall of its end in C++>=17 is if the
> program already had undefined behavior.

That's true if you use -fstrict-enums, and if you do that, there's no warning.
Without that option GCC does not treat (E)2 as undefined, and so it considers
it possible for the function to be called with that value.

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
                   ` (2 preceding siblings ...)
  2021-05-20 20:20 ` redi at gcc dot gnu.org
@ 2021-05-20 21:14 ` gonzalo.gadeschi at gmail dot com
  2021-05-21 14:21 ` egallager at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gonzalo.gadeschi at gmail dot com @ 2021-05-20 21:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

gnzlbg <gonzalo.gadeschi at gmail dot com> changed:

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

--- Comment #4 from gnzlbg <gonzalo.gadeschi at gmail dot com> ---
> That's true if you use -fstrict-enums,

Wasn't aware of this. Thanks, closing as invalid.

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
                   ` (3 preceding siblings ...)
  2021-05-20 21:14 ` gonzalo.gadeschi at gmail dot com
@ 2021-05-21 14:21 ` egallager at gcc dot gnu.org
  2021-05-21 16:51 ` harald at gigawatt dot nl
  2021-05-23 14:32 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: egallager at gcc dot gnu.org @ 2021-05-21 14:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #5 from Eric Gallager <egallager at gcc dot gnu.org> ---
Maybe the warning text should mention -fstrict-enums to make more people aware
of it?

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
                   ` (4 preceding siblings ...)
  2021-05-21 14:21 ` egallager at gcc dot gnu.org
@ 2021-05-21 16:51 ` harald at gigawatt dot nl
  2021-05-23 14:32 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: harald at gigawatt dot nl @ 2021-05-21 16:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #6 from Harald van Dijk <harald at gigawatt dot nl> ---
If the warning should mention -fstrict-enums, it should only do it in specific
cases:

enum E { A, B, C };

int h(E e) {
  switch (e) {
  case A: return 0;
  case B: return 0;
  case C: return 0;
  }
}

will still trigger the warning even with -fstrict-enums because now (E)3 is
valid.

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

* [Bug c++/100700] -Wreturn-type has many false positives
  2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
                   ` (5 preceding siblings ...)
  2021-05-21 16:51 ` harald at gigawatt dot nl
@ 2021-05-23 14:32 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-23 14:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100700

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Right, and of course there are many -Wreturn-type cases where no switches or
enums are involved at all.

The warning is about the missing return from the end of the function, not about
the switch that happens before that. Determining that the end of the function
was reached because of a misunderstanding about how enumerations work (either
in the standard, or in GCC without -fstrict-enums) is probably not easy.

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

end of thread, other threads:[~2021-05-23 14:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-20 10:33 [Bug c++/100700] New: -Wreturn-type has many false positives gonzalo.gadeschi at gmail dot com
2021-05-20 17:58 ` [Bug c++/100700] " msebor at gcc dot gnu.org
2021-05-20 19:09 ` gonzalo.gadeschi at gmail dot com
2021-05-20 20:20 ` redi at gcc dot gnu.org
2021-05-20 21:14 ` gonzalo.gadeschi at gmail dot com
2021-05-21 14:21 ` egallager at gcc dot gnu.org
2021-05-21 16:51 ` harald at gigawatt dot nl
2021-05-23 14:32 ` redi at gcc dot gnu.org

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).