c++: improve -Waddress warnings with *_cast [PR105569]
authorMarek Polacek <polacek@redhat.com>
Wed, 11 May 2022 18:38:49 +0000 (14:38 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 26 May 2022 21:11:07 +0000 (17:11 -0400)
commit6f56efa94e845db0d5c934ca202295019bf334c1
treeb23418750a1e6000006263be1144f8c877d9b716
parent3397563ad6c8fc5d9675faf507e52dd2ed284202
c++: improve -Waddress warnings with *_cast [PR105569]

This patch improves the diagnostic for -Waddress when it warns for

  if (dynamic_cast<A*>(&ref))
    // ...

where 'ref' is a reference, which cannot be null.  In particular, it
changes
warning: comparing the result of pointer addition '(((A*)ref) + ((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL
to
warning: the compiler can assume that the address of 'ref' will never be NULL

PR c++/105569

gcc/cp/ChangeLog:

* typeck.cc (warn_for_null_address): Improve the warning when
the POINTER_PLUS_EXPR's base is of reference type.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Waddress-9.C: New test.
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/warn/Waddress-9.C [new file with mode: 0644]