bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / test / unnecessaryparen.cxx
blobfb36052778b5c3f4e9e5f092e7951a4c090e97c1
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 <memory>
11 #include <string>
12 #include <rtl/ustring.hxx>
13 #include <o3tl/typed_flags_set.hxx>
15 #define MACRO (1)
17 bool foo(int);
19 enum class EFoo { Bar };
21 struct S { operator bool(); };
23 enum class BrowseMode
25 Modules = 0x01,
26 Top = 0x02,
27 Bottom = 0x04,
28 Left = 0x04,
30 namespace o3tl
32 template <> struct typed_flags<BrowseMode> : is_typed_flags<BrowseMode, 0xf>
37 int main()
39 int x = 1;
40 x = ((2)); // expected-error {{parentheses around parentheses [loplugin:unnecessaryparen]}}
42 if ((foo(1))) foo(2); // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
44 foo((1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
46 int y = (x); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
47 (void)y;
49 EFoo efoo = EFoo::Bar;
50 switch (efoo) {
51 case (EFoo::Bar): break; // expected-error {{parentheses immediately inside case statement [loplugin:unnecessaryparen]}}
54 int z = (y) ? 1 : 0; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
55 (void)z;
57 int v1 = (static_cast<short>(1)) + 1; // expected-error {{unnecessary parentheses around cast [loplugin:unnecessaryparen]}}
58 (void)v1;
60 // No warnings, used to silence -Wunreachable-code:
61 if ((false)) {
62 return 0;
64 x = (true) ? 0 : 1;
66 // More "no warnings", at least potentially used to silence -Wunreachable-code:
67 while ((false)) {
68 return 0;
70 for (; (false);) {
71 return 0;
73 x = foo(0) && (false) ? 0 : 1;
74 x = MACRO < (0) ? 0 : 1;
75 // cf. odd Clang -Wunreachable-code--suppression mechanism when the macro itself contains
76 // parentheses, causing the issue that lead to c421ac3f9432f2e9468d28447dc4c2e45b6f4da3
77 // "Revert loplugin:unnecessaryparen warning around integer literals"
79 int v2 = (1); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
80 (void)v2;
82 std::string v3;
83 v3 = (std::string("xx") + "xx"); // expected-error {{parentheses immediately inside assignment [loplugin:unnecessaryparen]}}
84 (void)v3;
86 S s1;
87 if ((s1)) { // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
88 return 0;
90 S s2;
91 if ((s2 = s1)) {
92 return 0;
95 (void) sizeof (int);
96 (void) sizeof (x); // expect no warning (for whatever reason; for symmetry with above case?)
98 // Expecting just one error, not reported twice during TraverseInitListExpr:
99 int a[] = {(x)}; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
100 (void) a;
102 (void) (+1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
103 (void) (-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
105 // For simplicity's sake, even warn about pathological cases that would require adding
106 // whitespace when removing the parentheses (as is also necessary in other cases anyway, like
107 // "throw(x);"); it is unlikely that there are any actual occurrences of code like "-(-1)" that
108 // would benefit from the parentheses readability-wise, compared to "- -1":
109 (void) -(-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
111 char *p = nullptr;
112 delete (p); // expected-error {{parentheses immediately inside delete expr [loplugin:unnecessaryparen]}}
114 BrowseMode nBits = ( BrowseMode::Modules | BrowseMode::Top ); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
115 (void)nBits;
118 class Foo2
120 int* p;
122 int foo2()
124 return (p) ? 1 : 0; // expected-error {{unnecessary parentheses around member expr [loplugin:unnecessaryparen]}}
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */