https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108416
Bug ID: 108416 Summary: False positive -Wdangling-pointer Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: steveire at gmail dot com Target Milestone: --- ``` #include <iostream> class HoldsNonOwning { public: void reset(int* nonOwning) { mNonOwning = nonOwning; } int compute(int input) { if (!mNonOwning) { return -1; } return input * *mNonOwning; } int* mNonOwning; }; class HolderTest { public: HoldsNonOwning holder; int compute(); }; int HolderTest::compute() { int nonOwning = 2; holder.reset(&nonOwning); int i = holder.compute(42); // If we uncomment this we don't get the warning // std::cout << i << "\n"; return i; } int main(int, char**) { HolderTest ht; int i = ht.compute(); std::cout << i << "\n"; return 0; } ``` The -Wdangling-pointer gets triggered on this reasonable code. The pointer is non-owning.