1*89a1d03eSRichard // RUN: %check_clang_tidy %s bugprone-misplaced-pointer-arithmetic-in-alloc %t 2*89a1d03eSRichard 3*89a1d03eSRichard class C { 4*89a1d03eSRichard int num; 5*89a1d03eSRichard public: C(int n)6*89a1d03eSRichard explicit C(int n) : num(n) {} 7*89a1d03eSRichard }; 8*89a1d03eSRichard bad_new(int n,int m)9*89a1d03eSRichardvoid bad_new(int n, int m) { 10*89a1d03eSRichard C *p = new C(n) + 10; 11*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument 12*89a1d03eSRichard // CHECK-FIXES: C *p = new C(n + 10); 13*89a1d03eSRichard 14*89a1d03eSRichard p = new C(n) - 10; 15*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument 16*89a1d03eSRichard // CHECK-FIXES: p = new C(n - 10); 17*89a1d03eSRichard 18*89a1d03eSRichard p = new C(n) + m; 19*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument 20*89a1d03eSRichard // CHECK-FIXES: p = new C(n + m); 21*89a1d03eSRichard 22*89a1d03eSRichard p = new C(n) - (m + 10); 23*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument 24*89a1d03eSRichard // CHECK-FIXES: p = new C(n - (m + 10)); 25*89a1d03eSRichard 26*89a1d03eSRichard p = new C(n) - m + 10; 27*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument 28*89a1d03eSRichard // CHECK-FIXES: p = new C(n - m) + 10; 29*89a1d03eSRichard // FIXME: Should be p = new C(n - m + 10); 30*89a1d03eSRichard } 31*89a1d03eSRichard bad_new_array(int n,int m)32*89a1d03eSRichardvoid bad_new_array(int n, int m) { 33*89a1d03eSRichard char *p = new char[n] + 10; 34*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument 35*89a1d03eSRichard // CHECK-FIXES: char *p = new char[n + 10]; 36*89a1d03eSRichard 37*89a1d03eSRichard p = new char[n] - 10; 38*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument 39*89a1d03eSRichard // CHECK-FIXES: p = new char[n - 10]; 40*89a1d03eSRichard 41*89a1d03eSRichard p = new char[n] + m; 42*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument 43*89a1d03eSRichard // CHECK-FIXES: p = new char[n + m]; 44*89a1d03eSRichard 45*89a1d03eSRichard p = new char[n] - (m + 10); 46*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument 47*89a1d03eSRichard // CHECK-FIXES: p = new char[n - (m + 10)]; 48*89a1d03eSRichard 49*89a1d03eSRichard p = new char[n] - m + 10; 50*89a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument 51*89a1d03eSRichard // CHECK-FIXES: p = new char[n - m] + 10; 52*89a1d03eSRichard // FIXME: should be p = new char[n - m + 10]; 53*89a1d03eSRichard } 54*89a1d03eSRichard 55*89a1d03eSRichard namespace std { 56*89a1d03eSRichard typedef decltype(sizeof(void*)) size_t; 57*89a1d03eSRichard } 58*89a1d03eSRichard 59*89a1d03eSRichard void* operator new(std::size_t, void*); 60*89a1d03eSRichard placement_new_ptr(void * buf,C * old)61*89a1d03eSRichardvoid placement_new_ptr(void *buf, C *old) { 62*89a1d03eSRichard C **p = new (buf) C*(old) + 1; 63*89a1d03eSRichard // CHECK-MESSAGES-NOT: :[[@LINE-1]]:11: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument 64*89a1d03eSRichard } 65