xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-do-while.cpp (revision 1af159e98c23a293c103e1f548866488126ed6f6)
1 // RUN: %check_clang_tidy -check-suffixes=DEFAULT       %s cppcoreguidelines-avoid-do-while %t
2 // RUN: %check_clang_tidy -check-suffixes=IGNORE-MACROS %s cppcoreguidelines-avoid-do-while %t -- -config='{CheckOptions: {cppcoreguidelines-avoid-do-while.IgnoreMacros: true}}'
3 
4 #define FOO(x) \
5   do { \
6   } while (x != 0)
7 
8 #define BAR_0(x) \
9   do { \
10     bar(x); \
11   } while (0)
12 
13 #define BAR_FALSE(x) \
14   do { \
15     bar(x); \
16   } while (false)
17 
18 void bar(int);
19 int baz();
20 
foo()21 void foo()
22 {
23     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+2]]:5: warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]
24     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]
25     do {
26 
27     } while(0);
28 
29     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+2]]:5: warning: avoid do-while loops
30     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
31     do {
32 
33     } while(1);
34 
35     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+2]]:5: warning: avoid do-while loops
36     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
37     do {
38 
39     } while(-1);
40 
41     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+2]]:5: warning: avoid do-while loops
42     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
43     do {
44 
45     } while(false);
46 
47     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+2]]:5: warning: avoid do-while loops
48     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
49     do {
50 
51     } while(true);
52 
53     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+3]]:5: warning: avoid do-while loops
54     // CHECK-MESSAGES-DEFAULT: :[[@LINE+2]]:5: warning: avoid do-while loops
55     int x1{0};
56     do {
57       x1 = baz();
58     } while (x1 > 0);
59 
60     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+2]]:5: warning: avoid do-while loops
61     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
62     do {
63 
64     } while (x1 != 0);
65 
66     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+3]]:5: warning: avoid do-while loops
67     // CHECK-MESSAGES-DEFAULT: :[[@LINE+2]]:5: warning: avoid do-while loops
68     constexpr int x2{0};
69     do {
70 
71     } while (x2);
72 
73     // CHECK-MESSAGES-IGNORE-MACROS: :[[@LINE+3]]:5: warning: avoid do-while loops
74     // CHECK-MESSAGES-DEFAULT: :[[@LINE+2]]:5: warning: avoid do-while loops
75     constexpr bool x3{false};
76     do {
77 
78     } while (x3);
79 
80     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
81     FOO(x1);
82 
83     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
84     BAR_0(x1);
85 
86     // CHECK-MESSAGES-DEFAULT: :[[@LINE+1]]:5: warning: avoid do-while loops
87     BAR_FALSE(x1);
88 }
89