llvm-project
54 строки · 1.5 Кб
1// RUN: %clang_cc1 -fsyntax-only -verify -Wdangling-else %s
2
3void f(int a, int b, int c, int d, int e) {4
5// should warn6{ 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
12if (a)13if (b) {14d++;15} else e++; // expected-warning {{add explicit braces to avoid dangling else}}16
17// shouldn't18{ 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
24if (a) {25if (b) d++;26else e++;27}28
29if (a) {30if (b) d++;31} else e++;32}
33
34// Somewhat more elaborate case that shouldn't warn.
35class A {36public:37void operator<<(const char* s) {}38};39
40void HandleDisabledThing() {}41A GetThing() { return A(); }42
43#define FOO(X) \44switch (0) default: \45if (!(X)) \46HandleDisabledThing(); \47else \48GetThing()49
50void f(bool cond) {51int x = 0;52if (cond)53FOO(x) << "hello"; // no warning54}
55
56