1 // RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t 2 3 enum E { 4 ENUM_LITERAL = 1 5 }; 6 7 int i = 4; 8 int j = 1; 9 int *p = 0; 10 int *q = 0; 11 12 void fail() { 13 q = p + 4; 14 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic] 15 p = q + i; 16 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic 17 p = q + ENUM_LITERAL; 18 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic 19 20 q = p - 1; 21 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic 22 p = q - i; 23 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic 24 p = q - ENUM_LITERAL; 25 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic 26 27 p += 4; 28 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic 29 p += i; 30 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic 31 p += ENUM_LITERAL; 32 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic 33 34 q -= 1; 35 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic 36 q -= i; 37 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic 38 q -= ENUM_LITERAL; 39 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic 40 41 p++; 42 // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic 43 ++p; 44 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic 45 46 p--; 47 // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic 48 --p; 49 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic 50 51 i = p[1]; 52 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic 53 } 54 55 struct S { 56 operator int() const; 57 }; 58 59 void f(S &s) { 60 int *i; 61 i = i + s; 62 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic 63 } 64 65 void f2(int i[]) { 66 i[1] = 0; 67 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic 68 } 69 70 void okay() { 71 int a[3]; 72 i = a[2]; // OK, access to array 73 74 p = q; 75 p = &i; 76 77 i++; 78 ++i; 79 i--; 80 --i; 81 i += 1; 82 i -= 1; 83 i = j + 1; 84 i = j - 1; 85 86 auto diff = p - q; // OK, result is arithmetic 87 88 for(int ii : a) ; // OK, pointer arithmetic generated by compiler 89 } 90