[Bug c++/99565] Bogus identical branches warning when returning references to union members

johelegp at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Mar 15 16:49:16 GMT 2021


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

--- Comment #2 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
```
<source>: In member function 'int& A::x()':
<source>:6:23: warning: this condition has identical branches
[-Wduplicated-branches]
    6 |   int& x() { return 0 ? a : b; }
      |                     ~~^~~~~~~
```

My actual use case looks more like this: https://godbolt.org/z/W5ajeM.
```C++
struct A {
  char x;
  long long y;
};
struct B {
  double x;
  long long y;
};
struct C {
  int i;
  union {
    A a;
    B b;
  };
  long long& y() { return i ? a.y : b.y; }
};
```
```
<source>: In member function 'long long int& C::y()':
<source>:15:29: warning: this condition has identical branches
[-Wduplicated-branches]
   15 |   long long& y() { return i ? a.y : b.y; }
      |                           ~~^~~~~~~~~~~
```


More information about the Gcc-bugs mailing list