Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / test / flatten.cxx
blob300067b9bfa10f9f9850d963646290f4516a4561
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <exception>
12 extern int foo();
13 extern int bar(int = 0);
14 class Class {};
16 void top1() {
17 if (foo() == 1) { // expected-note {{if condition here [loplugin:flatten]}}
18 foo();
19 } else {
20 throw std::exception(); // expected-error {{unconditional throw in else branch, rather invert the condition, throw early, and flatten the normal case [loplugin:flatten]}}
22 if (foo() == 1) {
23 Class aClass;
24 (void)aClass;
25 } else {
26 throw std::exception(); // no warning expected
28 if (foo() == 1) { // expected-note {{if condition here [loplugin:flatten]}}
29 Class aClass;
30 (void)aClass;
31 } else {
32 throw std::exception(); // expected-error {{unconditional throw in else branch, rather invert the condition, throw early, and flatten the normal case [loplugin:flatten]}}
36 void top2() {
37 if (foo() == 2) {
38 throw std::exception(); // expected-error {{unconditional throw in then branch, just flatten the else [loplugin:flatten]}}
39 } else {
40 foo();
42 if (foo() == 2) {
43 throw std::exception(); // no warning expected
44 } else {
45 Class aClass;
46 (void)aClass;
48 if (foo() == 2) {
49 throw std::exception(); // expected-error {{unconditional throw in then branch, just flatten the else [loplugin:flatten]}}
50 } else {
51 Class aClass;
52 (void)aClass;
56 void top3() {
57 // no warning expected
58 if (foo() == 2) {
59 throw std::exception();
60 } else {
61 Class aClass;
62 (void)aClass;
64 int x = 1;
65 (void)x;
68 void top4() {
69 // no warning expected
70 if (foo() == 2) {
71 Class aClass;
72 (void)aClass;
73 } else {
74 throw std::exception();
76 int x = 1;
77 (void)x;
80 void top5() {
81 #if 1
82 if (foo() == 2) {
83 if (foo() == 3) { // expected-note {{if condition here [loplugin:flatten]}}
84 bar();
85 } else {
86 throw std::exception(); // expected-error {{unconditional throw in else branch, rather invert the condition, throw early, and flatten the normal case [loplugin:flatten]}}
88 } else
89 #endif
90 throw std::exception(); // no warning expected
93 int main() {
94 // no warning expected
95 if (bar() == 3) {
96 throw std::exception();
97 } else {
98 throw std::exception();
102 void top6() {
103 // no warning expected
104 if (foo() == 2) {
105 Class aClass;
106 (void)aClass;
107 } else if (foo() == 2) {
108 Class aClass;
109 (void)aClass;
110 } else {
111 throw std::exception();
113 int x = 1;
114 (void)x;
117 void top7() {
118 // no warning expected
119 if (foo() == 1) {
120 throw std::exception();
121 } else if (foo() == 2) {
122 throw std::exception();
123 } else {
124 throw std::exception();
128 void top8() {
129 if (foo() == 1) {
130 if (foo() == 2) {
131 throw std::exception(); // expected-error {{unconditional throw in then branch, just flatten the else [loplugin:flatten]}}
132 } else {
133 bar();
135 } else if (foo() == 2) {
136 bar(1);
137 } else {
138 bar(2);
142 void top9() {
143 if (foo() == 1) { // expected-error {{large if statement at end of function, rather invert the condition and exit early, and flatten the function [loplugin:flatten]}}
144 Class aClass1;
145 (void)aClass1;
146 Class aClass2;
147 (void)aClass2;
148 Class aClass3;
149 (void)aClass3;
150 Class aClass4;
151 (void)aClass4;
152 Class aClass5;
153 (void)aClass5;
154 Class aClass6;
155 (void)aClass6;
159 void top10() {
160 // no warning expected
161 if (foo() == 2) {
162 if (foo() == 1) {
163 Class aClass1;
164 (void)aClass1;
165 Class aClass2;
166 (void)aClass2;
167 Class aClass3;
168 (void)aClass3;
173 int top11() {
174 // no warning expected
175 if (foo() == 1) {
176 Class aClass1;
177 (void)aClass1;
178 Class aClass2;
179 (void)aClass2;
180 Class aClass3;
181 (void)aClass3;
182 Class aClass4;
183 (void)aClass4;
184 Class aClass5;
185 (void)aClass5;
186 Class aClass6;
187 (void)aClass6;
189 return 1;
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */