public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99938] New: Non-void function with no return statement: Either no or misleading warning is printed
@ 2021-04-06 15:37 rschoe at de dot ibm.com
2021-04-06 15:41 ` [Bug c++/99938] " redi at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: rschoe at de dot ibm.com @ 2021-04-06 15:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99938
Bug ID: 99938
Summary: Non-void function with no return statement: Either no
or misleading warning is printed
Product: gcc
Version: 10.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: rschoe at de dot ibm.com
Target Milestone: ---
Created attachment 50513
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50513&action=edit
Code example when compiled with g++ -O1 -c code.cpp does not show any warning,
If you exchange NULL with nullptr, warning shows wrong line
Hi,
Tested this with g++ (GCC) 10.2.1 20201125 (Red Hat 10.2.1-9) uname -r
5.10.19-200.fc33.x86_64
The following code
```
#include <cstddef>
struct C
{
C(int *);
~C();
};
int foo()
{
C c = NULL;
if(false)
{
while(1){}
}
}
```
compiled with
`g++ -O1 -c code.cpp`
(compiler arguments are relevant)
does not generate any warning about missing return statement in `foo()`
however when modified slightly (change `NULL` to `nullptr`):
```
#include <cstddef>
struct C
{
C(int *);
~C();
};
int foo()
{
C c = nullptr;
if(false)
{
while(1){}
}
}
```
g++ generates the following output (compiler arguments are relevant):
```
g++ -O1 -c code.cpp
main.cpp: In function ‘int foo()’:
main.cpp:11:11: warning: control reaches end of non-void function
[-Wreturn-type]
11 | C c = nullptr;
| ^~~~~~~
```
which detects the missing `return` but points to the wrong line. I expected
line 16 (the closing bracket of foo() function scope) to be called out.
Other modifications which lead to the warning being printed with correct line
number (16) are (applying one at a time is sufficient):
- Compile with `-O0`
- Comment/remove the `while(1){}`
- Comment/remove the desctuctor `~C` declaration
clang prints warnings with correct line (16) in all cases. I would expect g++
to behave the same.
----
Excuse me if I overlooked something or misunderstood c++ or the concept of g++.
If this is intended behavior, I would be happy to learn more about it :)
Also I had some trouble formatting this bug report. Somehow I could not figure
out how to add formatting (e.g. Markdown) or attach multiple files.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-04-08 11:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-06 15:37 [Bug c++/99938] New: Non-void function with no return statement: Either no or misleading warning is printed rschoe at de dot ibm.com
2021-04-06 15:41 ` [Bug c++/99938] " redi at gcc dot gnu.org
2021-04-06 15:48 ` rschoe at de dot ibm.com
2021-04-08 11:19 ` marxin 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).