1 // RUN: %clang_cc1 -std=c++17 -fcxx-exceptions -fexceptions -fsyntax-only -Wunreachable-code-aggressive -fblocks -verify %s
2
3 int j;
4 int bar();
test1()5 int test1() {
6 for (int i = 0;
7 i != 10;
8 ++i) { // expected-warning {{loop will run at most once (loop increment never executed)}}
9 if (j == 23) // missing {}'s
10 bar();
11 return 1;
12 }
13 return 0;
14 return 1; // expected-warning {{will never be executed}}
15 }
16
test1_B()17 int test1_B() {
18 for (int i = 0;
19 i != 10;
20 ++i) { // expected-warning {{loop will run at most once (loop increment never executed)}}
21 if (j == 23) // missing {}'s
22 bar();
23 return 1;
24 }
25 return 0;
26 return bar(); // expected-warning {{will never be executed}}
27 }
28
test2(int i)29 void test2(int i) {
30 switch (i) {
31 case 0:
32 break;
33 bar(); // expected-warning {{will never be executed}}
34 case 2:
35 switch (i) {
36 default:
37 a: goto a;
38 }
39 bar(); // expected-warning {{will never be executed}}
40 }
41 b: goto b;
42 bar(); // expected-warning {{will never be executed}}
43 }
44
test3()45 void test3() {
46 ^{ return;
47 bar(); // expected-warning {{will never be executed}}
48 }();
49 while (++j) {
50 continue;
51 bar(); // expected-warning {{will never be executed}}
52 }
53 }
54
55 // PR 6130 - Don't warn about bogus unreachable code with throw's and
56 // temporary objects.
57 class PR6130 {
58 public:
59 PR6130();
60 ~PR6130();
61 };
62
pr6130(unsigned i)63 int pr6130(unsigned i) {
64 switch(i) {
65 case 0: return 1;
66 case 1: return 2;
67 default:
68 throw PR6130(); // no-warning
69 }
70 }
71
72 extern "C" void foo(void);
73 extern "C" __attribute__((weak)) decltype(foo) foo;
74
weak_redecl()75 void weak_redecl() {
76 if (foo)
77 return;
78 bar(); // no-warning
79 }
80
81 namespace pr52103 {
82
83 void g(int a);
84
f(int a)85 void f(int a) {
86 if (a > 4) [[ likely ]] { // no-warning
87 return;
88 }
89
90 if (a > 4) [[ unlikely ]] { // no-warning
91 return;
92
93 return; // expected-warning {{will never be executed}}
94 }
95
96 [[clang::musttail]] return g(a); // no-warning
97
98 [[clang::musttail]] return g(a); // expected-warning {{will never be executed}}
99 }
100
101 }
102
103 namespace gh57123 {
foo()104 bool foo() {
105 if constexpr (true) {
106 if (true)
107 return true;
108 else
109 return false; // expected-warning {{will never be executed}}
110 }
111 else
112 return false; // no-warning
113 }
114
bar()115 bool bar() {
116 if (true)
117 return true;
118 else
119 return false; // expected-warning {{will never be executed}}
120 }
121
baz()122 bool baz() {
123 if constexpr (true)
124 return true;
125 else {
126 if (true)
127 return true;
128 else
129 return false; // expected-warning {{will never be executed}}
130 }
131 }
132 }
133