Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / compilerplugins / clang / test / unnecessaryparen.cxx
blob89ca84da6ab23755e6b13027080dad74e755b191
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 <cstddef>
11 #include <memory>
12 #include <string>
13 #include <rtl/ustring.hxx>
14 #include <o3tl/typed_flags_set.hxx>
16 #define MACRO (1)
18 bool foo(int);
20 enum class EFoo { Bar };
22 struct S { operator bool(); };
24 enum class BrowseMode
26 Modules = 0x01,
27 Top = 0x02,
28 Bottom = 0x04,
29 Left = 0x08,
31 namespace o3tl
33 template <> struct typed_flags<BrowseMode> : is_typed_flags<BrowseMode, 0xf>
38 void f(char const *);
39 char const * operator ""_s1(char const *, std::size_t);
40 #if __cplusplus >= 202002L
41 struct Str { constexpr Str(char const *) {} };
42 template<Str> char const * operator ""_s2();
43 #endif
45 int main()
47 int x = 1;
48 x = ((2)); // expected-error {{parentheses around parentheses [loplugin:unnecessaryparen]}}
50 if ((foo(1))) foo(2); // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
52 foo((1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
54 int y = (x); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
55 (void)y;
57 EFoo efoo = EFoo::Bar;
58 switch (efoo) {
59 case (EFoo::Bar): break; // expected-error {{parentheses immediately inside case statement [loplugin:unnecessaryparen]}}
62 int z = (y) ? 1 : 0; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
63 (void)z;
65 int v1 = (static_cast<short>(1)) + 1; // expected-error {{unnecessary parentheses around cast [loplugin:unnecessaryparen]}}
66 (void)v1;
68 // No warnings, used to silence -Wunreachable-code:
69 if ((false)) {
70 return 0;
72 x = (true) ? 0 : 1;
74 // More "no warnings", at least potentially used to silence -Wunreachable-code:
75 while ((false)) {
76 return 0;
78 for (; (false);) {
79 return 0;
81 x = foo(0) && (false) ? 0 : 1;
82 x = MACRO < (0) ? 0 : 1;
83 // cf. odd Clang -Wunreachable-code--suppression mechanism when the macro itself contains
84 // parentheses, causing the issue that lead to c421ac3f9432f2e9468d28447dc4c2e45b6f4da3
85 // "Revert loplugin:unnecessaryparen warning around integer literals"
87 int v2 = (1); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
88 (void)v2;
90 std::string v3;
91 v3 = (std::string("xx") + "xx"); // expected-error {{parentheses immediately inside assignment [loplugin:unnecessaryparen]}}
92 (void)v3;
94 S s1;
95 if ((s1)) { // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
96 return 0;
98 S s2;
99 if ((s2 = s1)) {
100 return 0;
103 (void) sizeof (int);
104 (void) sizeof (x); // expect no warning (for whatever reason; for symmetry with above case?)
106 // Expecting just one error, not reported twice during TraverseInitListExpr:
107 int a[] = {(x)}; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
108 (void) a;
110 (void) (+1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
111 (void) (-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
113 // For simplicity's sake, even warn about pathological cases that would require adding
114 // whitespace when removing the parentheses (as is also necessary in other cases anyway, like
115 // "throw(x);"); it is unlikely that there are any actual occurrences of code like "-(-1)" that
116 // would benefit from the parentheses readability-wise, compared to "- -1":
117 (void) -(-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
119 char *p = nullptr;
120 delete (p); // expected-error {{parentheses immediately inside delete expr [loplugin:unnecessaryparen]}}
122 BrowseMode nBits = ( BrowseMode::Modules | BrowseMode::Top ); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
123 (void)nBits;
125 OUString::number((v2+1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
127 (void) ("foo"); // expected-error {{unnecessary parentheses around single-token string literal [loplugin:unnecessaryparen]}}
128 (void) ("foo" "bar");
129 f(("foo")); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
130 f(("foo" "bar"));
131 (void) ("foo"_s1); // expected-error {{unnecessary parentheses around single-token string literal [loplugin:unnecessaryparen]}}
132 (void) ("foo" "bar"_s1);
133 f(("foo"_s1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
134 f(("foo" "bar"_s1));
135 #if __cplusplus >= 202002L
136 (void) ("foo"_s2); //TODO: expected error {{unnecessary parentheses around single-token string literal [loplugin:unnecessaryparen]}}
137 (void) ("foo" "bar"_s2);
138 f(("foo"_s2)); // TODO: expected error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
139 f(("foo" "bar"_s2));
140 #endif
143 struct B { operator bool() const; };
145 struct N { bool operator !(); };
147 class Foo2
149 int* p;
150 B b;
151 N n;
153 int foo2()
155 return (p) ? 1 : 0; // expected-error {{unnecessary parentheses around member expr [loplugin:unnecessaryparen]}}
158 static int foo3(Foo2 & foo) {
159 (void) !(foo.p);
160 (void) !(foo.b);
161 (void) !(foo.n);
162 return (foo.p) ? 1 : 0;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */