Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / test / unnecessaryparen.cxx
blob8621fe9e8746c9cf53d837d72a0a1a7769f975e9
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 <string>
11 #include <rtl/ustring.hxx>
13 #define MACRO (1)
15 bool foo(int);
17 enum class EFoo { Bar };
19 struct S { operator bool(); };
21 int main()
23 int x = 1;
24 x = ((2)); // expected-error {{parentheses around parentheses [loplugin:unnecessaryparen]}}
26 if ((foo(1))) foo(2); // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
28 foo((1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
30 int y = (x); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
31 (void)y;
33 EFoo efoo = EFoo::Bar;
34 switch (efoo) {
35 case (EFoo::Bar): break; // expected-error {{parentheses immediately inside case statement [loplugin:unnecessaryparen]}}
38 int z = (y) ? 1 : 0; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
39 (void)z;
41 int v1 = (static_cast<short>(1)) + 1; // expected-error {{unnecessary parentheses around cast [loplugin:unnecessaryparen]}}
42 (void)v1;
44 // No warnings, used to silence -Wunreachable-code:
45 if ((false)) {
46 return 0;
48 x = (true) ? 0 : 1;
50 // More "no warnings", at least potentially used to silence -Wunreachable-code:
51 while ((false)) {
52 return 0;
54 for (; (false);) {
55 return 0;
57 x = foo(0) && (false) ? 0 : 1;
58 x = MACRO < (0) ? 0 : 1;
59 // cf. odd Clang -Wunreachable-code--suppression mechanism when the macro itself contains
60 // parentheses, causing the issue that lead to c421ac3f9432f2e9468d28447dc4c2e45b6f4da3
61 // "Revert loplugin:unnecessaryparen warning around integer literals"
63 int v2 = (1); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
64 (void)v2;
66 std::string v3;
67 v3 = (std::string("xx") + "xx"); // expected-error {{parentheses immediately inside assignment [loplugin:unnecessaryparen]}}
68 (void)v3;
70 S s1;
71 if ((s1)) { // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
72 return 0;
74 S s2;
75 if ((s2 = s1)) {
76 return 0;
79 (void) sizeof (int);
80 (void) sizeof (x); // expect no warning (for whatever reason; for symmetry with above case?)
82 // Expecting just one error, not reported twice during TraverseInitListExpr:
83 int a[] = {(x)}; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
84 (void) a;
86 (void) (+1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
87 (void) (-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
89 // For simplicity's sake, even warn about pathological cases that would require adding
90 // whitespace when removing the parentheses (as is also necessary in other cases anyway, like
91 // "throw(x);"); it is unlikely that there are any actual occurrences of code like "-(-1)" that
92 // would benefit from the parentheses readability-wise, compared to "- -1":
93 (void) -(-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
95 char *p = nullptr;
96 delete (p); // expected-error {{parentheses immediately inside delete expr [loplugin:unnecessaryparen]}}
99 struct S2 {
100 S2& GetText();
101 void toChar();
103 void func2(S2 *p)
105 (p->GetText()).toChar(); // expected-error {{unnecessary parentheses around member expr [loplugin:unnecessaryparen]}}
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */