[Bug c++/105794] New: Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true)

kamil.sliwak at codepoets dot it gcc-bugzilla@gcc.gnu.org
Tue May 31 15:42:07 GMT 2022


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

            Bug ID: 105794
           Summary: Spurious "control reaches end of non-void function"
                    warning with a combination of destructor, try/catch
                    and if (true)
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kamil.sliwak at codepoets dot it
  Target Milestone: ---

After switching to GCC 12 I noticed that in several places in the C++ project
I'm working on I'm now getting the `control reaches end of non-void function`
warning. This seems like a regression since it's not happening with earlier GCC
versions or with latest Clang.

I managed to distill the problematic code into a small repro (see below) and
it's reproducible via https://godbolt.org so it does not seem dependent on my
specific environment.

The original code is more complicated but in the end a combination of three
things seems to be causing this:
1. A variable of a custom class type that has an explicitly defined destructor
(which can be empty though).
2. A try/catch block with a return.
3. A `throw` wrapped in a condition that's a compile-time constant.

Removing any of the above makes the warning go away.

### Environment
- GCC version: 12.1.0
- System: Arch Linux

### Repro
#### Command
```
g++ test.cpp
```

#### `test.cpp`
```
#include <exception>

class C {
public:
        ~C() {}
};

int foo()
{
        C c;

        try
        {
                return 1;
        }
        catch (std::exception const&)
        {
        }

        if (true)
                throw std::exception();
}

int main() {
    return 0;
}
```

#### Output
```
test.cpp: In function ‘int foo()’:
test.cpp:22:1: warning: control reaches end of non-void function
[-Wreturn-type]
   22 | }
      | ^
```


More information about the Gcc-bugs mailing list