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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |middle-end
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The reason for this is the same as in pr95461 comment #1.  Like -Warray-bounds,
for optimal results, -Wnull-dereference relies on optimization.  That includes
dead code elimination.  In the test case, since x1 is unused, it's eliminated
early on, well before the warning sees the code.

All these "late" warnings could also run earlier to detect more instances of
the problems, at the cost of issuing what most users view as false positives. 
Below is another example of such a trade-off.  Clang makes an effort to avoid
issuing these but a front end can only do that to a point.  To do much better
control and data flow analysis is required, which is what GCC does.

$ cat pr95473.c && clang -S -Wall -Wno-unused-variable -xc++ pr95473.c
void f ()
{ 
  if (0)
    int &x1 = *(int *)nullptr; // unreachable null reference (no warning)

}

void g ()
{
  const int i = 0;
  if (i)
    int &x1 = *(int *)nullptr; // unreachable null reference (no warning)

}

void h ()
{
  int i = 0;
  if (i)
    int &x1 = *(int *)nullptr; // unreachable null reference (warning)
}
pr95473.c:20:15: warning: binding dereferenced null pointer to reference has
      undefined behavior [-Wnull-dereference]
    int &x1 = *(int *)nullptr; // unreachable null reference (warning)
              ^~~~~~~~~~~~~~~
1 warning generated.

*** This bug has been marked as a duplicate of bug 95461 ***

Reply via email to