| 1 |
#include <mozilla/RefPtr.h> |
| 2 |
|
| 3 |
using namespace mozilla; |
| 4 |
|
| 5 |
struct RefCountedBase { |
| 6 |
void AddRef(); |
| 7 |
void Release(); |
| 8 |
|
| 9 |
void method_test(); |
| 10 |
}; |
| 11 |
|
| 12 |
struct RefCountedBaseHolder { |
| 13 |
RefPtr<RefCountedBase> GetRefCountedBase() const { |
| 14 |
return mRefCountedBase; |
| 15 |
} |
| 16 |
|
| 17 |
private: |
| 18 |
RefPtr<RefCountedBase> mRefCountedBase = MakeRefPtr<RefCountedBase>(); |
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
void test_arrow_temporary_new_refptr_function_style_cast() { |
| 23 |
RefPtr<RefCountedBase>(new RefCountedBase())->method_test(); // expected-warning {{performance issue: temporary 'RefPtr<RefCountedBase>' is only dereferenced here once which involves short-lived AddRef/Release calls}} |
| 24 |
} |
| 25 |
|
| 26 |
void test_arrow_temporary_new_refptr_brace() { |
| 27 |
RefPtr<RefCountedBase>{new RefCountedBase()}->method_test(); // expected-warning {{performance issue: temporary 'RefPtr<RefCountedBase>' is only dereferenced here once which involves short-lived AddRef/Release calls}} |
| 28 |
} |
| 29 |
|
| 30 |
void test_arrow_temporary_new_c_style_cast() { |
| 31 |
((RefPtr<RefCountedBase>)(new RefCountedBase()))->method_test(); // expected-warning {{performance issue: temporary 'RefPtr<RefCountedBase>' is only dereferenced here once which involves short-lived AddRef/Release calls}} |
| 32 |
} |
| 33 |
|
| 34 |
void test_arrow_temporary_new_static_cast() { |
| 35 |
static_cast<RefPtr<RefCountedBase>>(new RefCountedBase())->method_test(); // expected-warning {{performance issue: temporary 'RefPtr<RefCountedBase>' is only dereferenced here once which involves short-lived AddRef/Release calls}} |
| 36 |
} |
| 37 |
|
| 38 |
void test_arrow_temporary_new_refptr_makerefptr() { |
| 39 |
MakeRefPtr<RefCountedBase>()->method_test(); // expected-warning {{performance issue: temporary 'RefPtr<RefCountedBase>' is only dereferenced here once which involves short-lived AddRef/Release calls}} |
| 40 |
} |
| 41 |
|
| 42 |
void test_arrow_temporary_get_refptr_from_member_function() { |
| 43 |
const RefCountedBaseHolder holder; |
| 44 |
holder.GetRefCountedBase()->method_test(); // expected-warning {{performance issue: temporary 'RefPtr<RefCountedBase>' is only dereferenced here once which involves short-lived AddRef/Release calls}} expected-note {{consider changing function RefCountedBaseHolder::GetRefCountedBase to return a raw reference instead}} |
| 45 |
} |
| 46 |
|
| 47 |
void test_ref(RefCountedBase &aRefCountedBase); |
| 48 |
|
| 49 |
void test_star_temporary_new_refptr_function_style_cast() { |
| 50 |
// TODO: Should we warn about operator* as well? |
| 51 |
test_ref(*RefPtr<RefCountedBase>(new RefCountedBase())); |
| 52 |
} |