bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / test / simplifybool.cxx
blob906feabee96f76913e4c3925f5e8369edc760478
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 <rtl/ustring.hxx>
11 // expected-note@rtl/ustring.hxx:* 2 {{the presumed corresponding negated operator for 'rtl::OUString' and 'rtl::OUString' is declared here [loplugin:simplifybool]}}
12 #include <rtl/string.hxx>
13 // expected-note@rtl/string.hxx:* {{the presumed corresponding negated operator for 'rtl::OString' and 'rtl::OString' is declared here [loplugin:simplifybool]}}
14 #include <basegfx/vector/b3dvector.hxx>
15 // expected-note@basegfx/tuple/b3dtuple.hxx:* {{the presumed corresponding negated operator for 'basegfx::B3DVector' and 'basegfx::B3DVector' is declared here [loplugin:simplifybool]}}
17 #include <map>
19 namespace group1
21 void f1(int a, int b)
23 if (!(a < b))
24 { // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
25 a = b;
29 void f2(float a, float b)
31 // no warning expected
32 if (!(a < b))
34 a = b;
39 // Consistently either warn about all or none of the below occurrences of "!!":
40 namespace group2
42 enum E1
44 E1_1 = 1
47 enum E2
49 E2_1 = 1
51 E2 operator&(E2 e1, E2 e2);
52 bool operator!(E2 e);
54 enum class E3
56 E1 = 1
58 struct W
60 operator bool();
62 W operator&(E3 e1, E3 e2);
64 bool f0(int n) { return !!(n & 1); }
66 bool f1(E1 e) { return !!(e & E1_1); }
68 bool f2(E2 e) { return !!(e & E2_1); }
70 bool f3(E3 e) { return !!(e & E3::E1); }
73 // record types
74 namespace group3
76 struct Record1
78 bool operator==(const Record1&) const;
81 struct Record2
83 bool operator==(const Record2&) const;
84 bool operator!=(const Record2&) const;
85 // expected-note@-1 {{the presumed corresponding negated operator for 'group3::Record2' and 'group3::Record2' is declared here [loplugin:simplifybool]}}
88 struct Record3
92 bool operator==(const Record3&, const Record3&);
93 bool operator!=(const Record3&, const Record3&);
94 // expected-note@-1 {{the presumed corresponding negated operator for 'group3::Record3' and 'group3::Record3' is declared here [loplugin:simplifybool]}}
96 void testRecord()
98 Record1 a1;
99 Record1 a2;
100 // no warning expected, because a negated operator does not exist
101 bool v = !(a1 == a2);
102 Record2 b1;
103 Record2 b2;
104 v = !(b1 == b2);
105 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
106 Record3 c1;
107 Record3 c2;
108 v = !(c1 == c2);
109 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
110 OUString d1;
111 OUString d2;
112 v = !(d1 == d2);
113 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
114 OString e1;
115 OString e2;
116 v = !(e1 == e2);
117 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
119 // the operator != is in a base-class, and the param is a base-type
120 basegfx::B3DVector f1;
121 basegfx::B3DVector f2;
122 v = !(f1 == f2);
123 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
126 struct Record4
128 bool operator==(Record4 const&) const;
129 bool operator!=(Record4 const& other) const
131 // no warning expected
132 bool v = !operator==(other);
133 v = !(*this == other);
134 OUString c1;
135 OUString c2;
136 v = !(c1 == c2);
137 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
138 return v;
143 namespace group4
145 bool foo1(bool a, bool b)
147 return !(!a && !b);
148 // expected-error@-1 {{logical negation of logical op containing negation, can be simplified [loplugin:simplifybool]}}
150 bool foo2(int a, bool b)
152 return !(a != 1 && !b);
153 // expected-error@-1 {{logical negation of logical op containing negation, can be simplified [loplugin:simplifybool]}}
155 bool foo3(int a, bool b)
157 // no warning expected
158 return !(a != 1 && b);
162 namespace group5
164 bool foo1(std::map<int, int>* pActions, int aKey)
166 auto aIter = pActions->find(aKey);
167 //TODO this doesn't work yet because I'd need to implement conversion operators during method/func lookup
168 return !(aIter == pActions->end());
169 // expected-error@-1 {{logical negation of comparison operator, can be simplified by inverting operator [loplugin:simplifybool]}}
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */