xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/inc-dec-in-conditions.cpp (revision d4a4585165f5c6ca8c42920b70e1b47696ff1172)
1f27f22b3SPiotr Zegar // RUN: %check_clang_tidy %s bugprone-inc-dec-in-conditions %t
2f27f22b3SPiotr Zegar 
3f27f22b3SPiotr Zegar template<typename T>
4f27f22b3SPiotr Zegar struct Iterator {
5f27f22b3SPiotr Zegar   Iterator operator++(int);
6f27f22b3SPiotr Zegar   Iterator operator--(int);
7f27f22b3SPiotr Zegar   Iterator& operator++();
8f27f22b3SPiotr Zegar   Iterator& operator--();
9f27f22b3SPiotr Zegar   T operator*();
10f27f22b3SPiotr Zegar   bool operator==(Iterator) const;
11f27f22b3SPiotr Zegar   bool operator!=(Iterator) const;
12f27f22b3SPiotr Zegar };
13f27f22b3SPiotr Zegar 
14f27f22b3SPiotr Zegar template<typename T>
15f27f22b3SPiotr Zegar struct Container {
16f27f22b3SPiotr Zegar   Iterator<T> begin();
17f27f22b3SPiotr Zegar   Iterator<T> end();
18f27f22b3SPiotr Zegar };
19f27f22b3SPiotr Zegar 
f(int x)20f27f22b3SPiotr Zegar bool f(int x) {
21f27f22b3SPiotr Zegar   return (++x != 5 or x == 10);
22f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: incrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
23f27f22b3SPiotr Zegar }
24f27f22b3SPiotr Zegar 
f2(int x)25f27f22b3SPiotr Zegar bool f2(int x) {
26f27f22b3SPiotr Zegar   return (x++ != 5 or x == 10);
27f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: incrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
28f27f22b3SPiotr Zegar }
29f27f22b3SPiotr Zegar 
c(Container<int> x)30f27f22b3SPiotr Zegar bool c(Container<int> x) {
31f27f22b3SPiotr Zegar   auto it = x.begin();
32f27f22b3SPiotr Zegar   return (it++ != x.end() and *it);
33f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: incrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
34f27f22b3SPiotr Zegar }
35f27f22b3SPiotr Zegar 
c2(Container<int> x)36f27f22b3SPiotr Zegar bool c2(Container<int> x) {
37f27f22b3SPiotr Zegar   auto it = x.begin();
38f27f22b3SPiotr Zegar   return (++it != x.end() and *it);
39f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: incrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
40f27f22b3SPiotr Zegar }
41f27f22b3SPiotr Zegar 
d(int x)42f27f22b3SPiotr Zegar bool d(int x) {
43f27f22b3SPiotr Zegar   return (--x != 5 or x == 10);
44f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: decrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
45f27f22b3SPiotr Zegar }
46f27f22b3SPiotr Zegar 
d2(int x)47f27f22b3SPiotr Zegar bool d2(int x) {
48f27f22b3SPiotr Zegar   return (x-- != 5 or x == 10);
49f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: decrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
50f27f22b3SPiotr Zegar }
51f27f22b3SPiotr Zegar 
g(Container<int> x)52f27f22b3SPiotr Zegar bool g(Container<int> x) {
53f27f22b3SPiotr Zegar   auto it = x.begin();
54f27f22b3SPiotr Zegar   return (it-- != x.end() and *it);
55f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: decrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
56f27f22b3SPiotr Zegar }
57f27f22b3SPiotr Zegar 
g2(Container<int> x)58f27f22b3SPiotr Zegar bool g2(Container<int> x) {
59f27f22b3SPiotr Zegar   auto it = x.begin();
60f27f22b3SPiotr Zegar   return (--it != x.end() and *it);
61f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: decrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
62f27f22b3SPiotr Zegar }
63f27f22b3SPiotr Zegar 
doubleCheck(Container<int> x)64f27f22b3SPiotr Zegar bool doubleCheck(Container<int> x) {
65f27f22b3SPiotr Zegar   auto it = x.begin();
66f27f22b3SPiotr Zegar   auto it2 = x.begin();
67f27f22b3SPiotr Zegar   return (--it != x.end() and ++it2 != x.end()) and (*it == *it2);
68f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: decrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
69f27f22b3SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:31: warning: incrementing and referencing a variable in a complex condition can cause unintended side-effects due to C++'s order of evaluation, consider moving the modification outside of the condition to avoid misunderstandings [bugprone-inc-dec-in-conditions]
70f27f22b3SPiotr Zegar }
71*d4a45851SPiotr Zegar 
72*d4a45851SPiotr Zegar namespace PR85838 {
test()73*d4a45851SPiotr Zegar   void test()
74*d4a45851SPiotr Zegar   {
75*d4a45851SPiotr Zegar     auto foo = 0;
76*d4a45851SPiotr Zegar     auto bar = 0;
77*d4a45851SPiotr Zegar     if (++foo < static_cast<decltype(foo)>(bar)) {}
78*d4a45851SPiotr Zegar     if (static_cast<decltype(++foo)>(bar) < foo) {}
79*d4a45851SPiotr Zegar   }
80*d4a45851SPiotr Zegar }
81