143 lines · cpp
1// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic -check-suffixes=,DEFAULT %t2// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t -- \3// RUN: -config="{CheckOptions: {cppcoreguidelines-pro-bounds-pointer-arithmetic.AllowIncrementDecrementOperators: true}}" --4 5enum E {6 ENUM_LITERAL = 17};8 9typedef int* IntPtr;10 11int i = 4;12int j = 1;13int *p = 0;14int *q = 0;15IntPtr ip = 0;16 17void fail() {18 q = p + 4;19 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]20 p = q + i;21 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic22 p = q + ENUM_LITERAL;23 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic24 25 q = p - 1;26 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic27 p = q - i;28 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic29 p = q - ENUM_LITERAL;30 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic31 32 p += 4;33 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic34 p += i;35 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic36 p += ENUM_LITERAL;37 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic38 39 q -= 1;40 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic41 q -= i;42 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic43 q -= ENUM_LITERAL;44 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic45 46 p++;47 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic48 ++p;49 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: do not use pointer arithmetic50 51 p--;52 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic53 --p;54 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: do not use pointer arithmetic55 56 i = p[1];57 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic58 59 p = ip + 1;60 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not use pointer arithmetic61 ip++;62 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:5: warning: do not use pointer arithmetic63 i = ip[1];64 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic65}66 67template <typename T>68void template_fail() {69 T* p;70 T* q;71 72 p = q + 1;73 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic74 q = p - 1;75 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic76 p++;77 // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: do not use pointer arithmetic78 i = p[1];79 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic80}81 82void instantiate() {83 template_fail<int>();84}85 86struct S {87 operator int() const;88};89 90void f(S &s) {91 int *i;92 i = i + s;93 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic94}95 96void f2(int i[]) {97 i[1] = 0;98 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic99}100 101void okay() {102 int a[3];103 i = a[2]; // OK, access to array104 105 p = q;106 p = &i;107 108 i++;109 ++i;110 i--;111 --i;112 i += 1;113 i -= 1;114 i = j + 1;115 i = j - 1;116 117 auto diff = p - q; // OK, result is arithmetic118 119 for(int ii : a) ; // OK, pointer arithmetic generated by compiler120}121 122namespace gh126424 {123 124namespace std {125template <typename, typename>126class pair {};127 128template <typename Key, typename Value>129class map {130 public:131 using value_type = pair<Key, Value>;132 value_type& operator[](const Key& key);133 value_type& operator[](Key&& key);134 };135}136 137template <typename R>138int f(std::map<R*, int>& map, R* r) {139 return map[r]; // OK140}141 142}143