1// RUN: %check_clang_tidy -std=c++14-or-later %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
2
3// Fix PR36489 and detect auto-deduced value correctly.
4typedef char* charPtr;
5
6char *getPtr();
7auto getPtrAuto() { return getPtr(); }
8decltype(getPtr()) getPtrDeclType();
9decltype(auto) getPtrDeclTypeAuto() { return getPtr(); }
10auto getPtrWithTrailingReturnType() -> char *;
11charPtr getCharPtr() { return getPtr(); }
12
13void auto_deduction_binary() {
14 auto p1 = getPtr() + 1;
15 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not use pointer arithmetic
16 auto p2 = getPtrAuto() + 1;
17 // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not use pointer arithmetic
18 auto p3 = getPtrWithTrailingReturnType() + 1;
19 // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not use pointer arithmetic
20 auto p4 = getPtr();
21 auto *p5 = getPtr();
22 p4 = p4 + 1;
23 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not use pointer arithmetic
24 p5 = p5 + 1;
25 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not use pointer arithmetic
26 auto p6 = getPtrDeclType() + 1;
27 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: do not use pointer arithmetic
28 auto p7 = getPtrDeclTypeAuto() + 1;
29 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not use pointer arithmetic
30 auto *p8 = getPtrDeclType() + 1;
31 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: do not use pointer arithmetic
32 auto *p9 = getPtrDeclTypeAuto() + 1;
33 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: do not use pointer arithmetic
34 auto p10 = getCharPtr() + 1;
35 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use pointer
36 auto* p11 = getCharPtr() + 1;
37 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use pointer arithmetic
38}
39
40void auto_deduction_subscript() {
41 char p1 = getPtr()[2];
42 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
43 auto p2 = getPtr()[3];
44 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
45
46 char p3 = getPtrAuto()[4];
47 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
48 auto p4 = getPtrAuto()[5];
49 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
50
51 char p5 = getPtrWithTrailingReturnType()[6];
52 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
53 auto p6 = getPtrWithTrailingReturnType()[7];
54 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
55
56 auto p7 = getPtrDeclType()[8];
57 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
58 auto p8 = getPtrDeclTypeAuto()[9];
59 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
60 auto p9 = getCharPtr()[10];
61 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
62}
63

source code of clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-pr36489.cpp