From 32c8b03f6172acf3a19c5d1938b9935369f536f1 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Fri, 20 Jan 2023 12:55:25 +0200 Subject: improve loplugin:refcounting to catch places where we are converting a weak reference to a strong reference, and then using a pointer to store the result Change-Id: I69b132907b574e5c6974fadf18bd9658107d3a0d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145877 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/test/refcounting.cxx | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'compilerplugins/clang/test/refcounting.cxx') diff --git a/compilerplugins/clang/test/refcounting.cxx b/compilerplugins/clang/test/refcounting.cxx index 2b8ce94b42e6..54d4dbe14b38 100644 --- a/compilerplugins/clang/test/refcounting.cxx +++ b/compilerplugins/clang/test/refcounting.cxx @@ -107,11 +107,26 @@ void foo7() UnoSubObject* p3 = static_cast(getConstRef().get()); (void)p3; p3 = static_cast(getConstRef().get()); +} + +const unotools::WeakReference& getWeakRef(); +void foo8() +{ + // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}} + UnoSubObject* p1 = static_cast(getWeakRef().get().get()); + (void)p1; + + // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}} + UnoObject* p2 = getWeakRef().get().get(); + (void)p2; - // no warning expected, although, arguably, we should be assigning to a rtl::Reference temporary unotools::WeakReference weak1; - auto pTextObj = dynamic_cast(weak1.get().get()); - (void)pTextObj; -} + // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}} + UnoSubObject* p3 = dynamic_cast(weak1.get().get()); + (void)p3; + // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}} + UnoObject* p4 = weak1.get().get(); + (void)p4; +} /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit