207 lines · cpp
1// RUN: %check_clang_tidy %s readability-misleading-indentation %t -- -- -fno-delayed-template-parsing2 3void foo1();4void foo2();5void foo3();6 7#define E8 9#define BLOCK \10 if (cond1) \11 foo1(); \12 foo2();13 14void f()15{16 bool cond1 = true;17 bool cond2 = true;18 19 if (cond1)20 if (cond2)21 foo1();22 else23 foo2();24 // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]25 26 if (cond1) {27 if (cond2)28 foo1();29 }30 else31 foo2();32 33 if (cond1)34 if (cond2)35 foo1();36 else37 foo2();38 39 if (cond2)40 foo1();41 foo2();42 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: misleading indentation: statement is indented too deeply [readability-misleading-indentation]43 // CHECK-MESSAGES: :[[@LINE-4]]:3: note: did you mean this line to be inside this 'if'44 foo2(); // No redundant warning.45 46 if (cond1)47 {48 foo1();49 }50 foo2();51 52 if (cond1)53 foo1();54 foo2();55 56 if (cond2)57 if (cond1) foo1(); else foo2();58 59 if (cond1) {60 } else {61 }62 63 if (cond1) {64 }65 else {66 }67 68 if (cond1)69 {70 }71 else72 {73 }74 75 if (cond1)76 {77 }78 else79 {80 }81 82 if(cond1) {83 }84 else if (cond2) {85 }86 else {87 }88 89 if(cond1) {90 }91 else if (cond2) {92 }93 else {94 }95 // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]96 97 if (cond1) {98 if (cond1) {99 }100 else if (cond2) {101 }102 else {103 }104 if (cond1) {105 } else if (cond2) {106 } else if (!cond2) {107 } else {108 }109 }110 else if (cond2) {111 }112 113 BLOCK114 115 if (cond1)116 foo1();117 else118 foo2();119 E foo3();120 // CHECK-MESSAGES-NOT: :[[@LINE-1]]readability-misleading-indentation121}122 123void g(bool x) {124 if (x)125 #pragma unroll126 for (int k = 0; k < 1; ++k) {}127 128 #pragma unroll129 for (int k = 0; k < 1; ++k) {}130}131 132template<bool b>133void mustPass() {134 if constexpr (b) {135 foo1();136 } else {137 foo2();138 }139}140 141void mustPassNonTemplate() {142 constexpr unsigned Value = 1;143 if constexpr (Value == 0) {144 foo1();145 } else if constexpr (Value == 1) {146 foo2();147 } else {148 foo3();149 }150}151 152template<bool b>153void mustFail() {154 if constexpr (b) {155 foo1();156 }157 else {158 foo2();159 // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]160 }161}162 163void mustFailNonTemplate() {164 constexpr unsigned Value = 1;165 if constexpr (Value == 0) {166 foo1();167 }168 else {169 foo2();170 // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]171 }172 173 if constexpr (Value == 0)174 foo1();175 else176 foo2();177 // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]178}179 180template<bool b>181void mustFailNoInsta() {182 if constexpr (b) {183 foo1();184 }185 else {186 foo2();187 // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]188 }189}190 191template<bool b>192void mustPassNoInsta() {193 if constexpr (b) {194 foo1();195 }196 else {197 foo2();198 }199}200 201void call() {202 mustPass<true>();203 mustPass<false>();204 mustFail<true>();205 mustFail<false>();206}207