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