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

--- Comment #24 from Martin Sebor <msebor at gcc dot gnu.org> ---
I see.  Yes, if the types are unrelated, that would be a likely bug.   I think
could and should be diagnosed by the C++ front end, by some more targeted
warning than -Wnonnull (as requested in pr38557).

But I didn't actually mean to write that test case in comment #20 (I forgot to
derive one class from the other).  The following is what I meant where the
-Wnonnull is strictly a false positive because of the test.  It can be avoided
by casting *p to C& which might be argued (as I do in comment #17) makes the
code clearer, but if this is a common idiom we could try to suppress the
warning in these cases as well.  Do you see this pattern a lot?

struct A { virtual ~A (); };
struct C: A { virtual ~C (); void f (); };

void g (A *p)
{
  if (dynamic_cast<C*>(p))
    dynamic_cast<C*>(p)->f ();   // -Wnonnull

  /* Avoid like so:
  if (dynamic_cast<C*>(p))
    dynamic_cast<C&>(*p)->f ();  */

}

Reply via email to