1 // RUN: %clang_cc1 -fsyntax-only -verify -Wdangling-else %s 2 3 void f(int a, int b, int c, int d, int e) { 4 5 // should warn 6 { if (a) if (b) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}} 7 { if (a) while (b) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}} 8 { if (a) switch (b) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}} 9 { if (a) for (;;) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}} 10 { if (a) if (b) if (d) d++; else e++; else d--; } // expected-warning {{add explicit braces to avoid dangling else}} 11 12 if (a) 13 if (b) { 14 d++; 15 } else e++; // expected-warning {{add explicit braces to avoid dangling else}} 16 17 // shouldn't 18 { if (a) if (b) d++; } 19 { if (a) if (b) if (c) d++; } 20 { if (a) if (b) d++; else e++; else d--; } 21 { if (a) if (b) if (d) d++; else e++; else d--; else e--; } 22 { if (a) do if (b) d++; else e++; while (c); } 23 24 if (a) { 25 if (b) d++; 26 else e++; 27 } 28 29 if (a) { 30 if (b) d++; 31 } else e++; 32 } 33 34 // Somewhat more elaborate case that shouldn't warn. 35 class A { 36 public: 37 void operator<<(const char* s) {} 38 }; 39 40 void HandleDisabledThing() {} 41 A GetThing() { return A(); } 42 43 #define FOO(X) \ 44 switch (0) default: \ 45 if (!(X)) \ 46 HandleDisabledThing(); \ 47 else \ 48 GetThing() 49 50 void f(bool cond) { 51 int x = 0; 52 if (cond) 53 FOO(x) << "hello"; // no warning 54 } 55 56