public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108476] New: Please turn -Wreturn-type on by default for C
@ 2023-01-20  7:08 alexhenrie24 at gmail dot com
  2023-01-20  7:23 ` [Bug c/108476] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-01-20  7:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108476
           Summary: Please turn -Wreturn-type on by default for C
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexhenrie24 at gmail dot com
  Target Milestone: ---

According to the documentation, -Wreturn-type does the following:

    "Warn whenever a function is defined with a return type that defaults to
int. Also warn about any return statement with no return value in a function
whose return type is not void (falling off the end of the function body is
considered returning without a value)."

    https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

I feel strongly that gcc should turn on -Wreturn-type by default for the
following reasons:

* Not specifying the return value of a function that returns a non-void value
is almost always a bug.

* C99 and later do not allow declaring or defining a function without a return
type, so there's a good chance that code that omits the return type will break
when used with other compilers.

* GCC already turns the warning on by default for C++, and all but the most
educated of users do not expect C and C++ to be different in this regard, so it
gives a false sense of security when no such warnings appear when compiling C
code.

* Clang already turns the warning on by default for both C and C++, which goes
to show that the warning does not come up much in practice.

I've personally been bitten by forgetting to return a value from a function, so
I hope you will agree that -Wreturn-type is a sensible default to help spot
bugs sooner.

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

* [Bug c/108476] Please turn -Wreturn-type on by default for C
  2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
@ 2023-01-20  7:23 ` pinskia at gcc dot gnu.org
  2023-01-20  8:32 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-20  7:23 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=53232

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the warning should really be split into two different options. One for the
return type of the declaration and one for the missing return in non-void case.

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

* [Bug c/108476] Please turn -Wreturn-type on by default for C
  2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
  2023-01-20  7:23 ` [Bug c/108476] " pinskia at gcc dot gnu.org
@ 2023-01-20  8:32 ` rguenth at gcc dot gnu.org
  2023-01-20 10:13 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-01-20  8:32 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
           Severity|normal                      |enhancement

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

* [Bug c/108476] Please turn -Wreturn-type on by default for C
  2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
  2023-01-20  7:23 ` [Bug c/108476] " pinskia at gcc dot gnu.org
  2023-01-20  8:32 ` rguenth at gcc dot gnu.org
@ 2023-01-20 10:13 ` jakub at gcc dot gnu.org
  2023-01-23 16:40 ` alexhenrie24 at gmail dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-01-20 10:13 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The reason it is enabled by default for C++ is that the 2 languages differ
significantly in this regard.  Falling through the end of a non-void function
in C++ is undefined behavior, in C it is not, in C it is only UB if the caller
actually uses the uninitialized return value (which is much harder to warn
about).
And in C it is enabled in -Wall, which you should use anyway if you care about
warnings.

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

* [Bug c/108476] Please turn -Wreturn-type on by default for C
  2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
                   ` (2 preceding siblings ...)
  2023-01-20 10:13 ` jakub at gcc dot gnu.org
@ 2023-01-23 16:40 ` alexhenrie24 at gmail dot com
  2023-12-01  7:18 ` fw at gcc dot gnu.org
  2024-01-12 13:43 ` xry111 at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-01-23 16:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Alex Henrie <alexhenrie24 at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> Note the warning should really be split into two different options. One for
> the return type of the declaration and one for the missing return in
> non-void case.
That would be nice, I agree. I'd just like to note that since the warning
should occur by default in both situations, this feature request does not
depend on splitting the warning in two.

(In reply to Jakub Jelinek from comment #2)
> The reason it is enabled by default for C++ is that the 2 languages differ
> significantly in this regard.  Falling through the end of a non-void
> function in C++ is undefined behavior, in C it is not, in C it is only UB if
> the caller actually uses the uninitialized return value (which is much
> harder to warn about).
Yes, I did read the note about that in the documentation (although I didn't
quote that part in comment #0). You're right that it's slightly less bad in C
because not specifying the return value is immediately undefined behavior in
C++, whereas in C it only becomes undefined behavior once the return value is
used. However, few people know about that subtle difference between C and C++
(which leads to a false sense of security when the warning does not appear in
C), and not specifying the return value will almost certainly lead to undefined
behavior in C even though technically there are situations where it does not.

> And in C it is enabled in -Wall, which you should use anyway if you care
> about warnings.
I do use -Wall whenever I can. Unfortunately, not everyone does (particularly
novices or people stuck with awful embedded-toolchain IDEs that don't make it
easy to change compiler settings), so I'd like the default to be both less
confusing and more protective against likely undefined behavior.

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

* [Bug c/108476] Please turn -Wreturn-type on by default for C
  2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
                   ` (3 preceding siblings ...)
  2023-01-23 16:40 ` alexhenrie24 at gmail dot com
@ 2023-12-01  7:18 ` fw at gcc dot gnu.org
  2024-01-12 13:43 ` xry111 at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: fw at gcc dot gnu.org @ 2023-12-01  7:18 UTC (permalink / raw)
  To: gcc-bugs

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

Florian Weimer <fw at gcc dot gnu.org> changed:

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

--- Comment #4 from Florian Weimer <fw at gcc dot gnu.org> ---
For GCC 14, we have -Wreturn-mismatch turned on by default, as an error. This
diagnoses cases of extra or missing expressions in return statements. Together
with the other warnings-as-errors changes, this seems to cover the request in
the description.

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

* [Bug c/108476] Please turn -Wreturn-type on by default for C
  2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
                   ` (4 preceding siblings ...)
  2023-12-01  7:18 ` fw at gcc dot gnu.org
@ 2024-01-12 13:43 ` xry111 at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-01-12 13:43 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

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

--- Comment #5 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Florian Weimer from comment #4)
> For GCC 14, we have -Wreturn-mismatch turned on by default, as an error.
> This diagnoses cases of extra or missing expressions in return statements.
> Together with the other warnings-as-errors changes, this seems to cover the
> request in the description.

It only catches

int f() { return; }

not a plain

int f() {}

AFAIK the standard (even the latest C23) still disallows us from simply
rejecting the latter.

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

end of thread, other threads:[~2024-01-12 13:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-20  7:08 [Bug c/108476] New: Please turn -Wreturn-type on by default for C alexhenrie24 at gmail dot com
2023-01-20  7:23 ` [Bug c/108476] " pinskia at gcc dot gnu.org
2023-01-20  8:32 ` rguenth at gcc dot gnu.org
2023-01-20 10:13 ` jakub at gcc dot gnu.org
2023-01-23 16:40 ` alexhenrie24 at gmail dot com
2023-12-01  7:18 ` fw at gcc dot gnu.org
2024-01-12 13:43 ` xry111 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).