From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5DF7E3971430; Tue, 16 Feb 2021 20:14:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5DF7E3971430 From: "msebor at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/96003] [11 Regression] spurious -Wnonnull calling a member on the result of static_cast Date: Tue, 16 Feb 2021 20:14:24 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: diagnostic, patch X-Bugzilla-Severity: normal X-Bugzilla-Who: msebor at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: FIXED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: msebor at gcc dot gnu.org X-Bugzilla-Target-Milestone: 11.0 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Feb 2021 20:14:25 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96003 --- Comment #24 from Martin Sebor --- I see. Yes, if the types are unrelated, that would be a likely bug. I th= ink 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 avoi= ded 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(p)) dynamic_cast(p)->f (); // -Wnonnull /* Avoid like so: if (dynamic_cast(p)) dynamic_cast(*p)->f (); */ }=