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.

Reply via email to