Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / redundantfcast.cxx
bloba477e48f53083a0ae7e5f7ddfec5bed160c7eb53
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 "sal/config.h"
12 #include "rtl/ustring.hxx"
13 #include "tools/color.hxx"
15 #include <functional>
16 #include <initializer_list>
17 #include <memory>
19 void method1(OUString const&); // expected-note {{in call to method here [loplugin:redundantfcast]}}
21 struct Foo
23 Foo(int) {}
26 void func1(Foo const& f); // expected-note {{in call to method here [loplugin:redundantfcast]}}
28 namespace tools
30 struct Polygon
32 Polygon() = default;
34 struct PolyPolygon
36 PolyPolygon(
37 Polygon const&); // expected-note {{in call to method here [loplugin:redundantfcast]}}
41 void ImplWritePolyPolygonRecord(const tools::PolyPolygon& rPolyPoly);
43 int main()
45 OUString s;
46 (void)OUString(
47 s); // expected-error-re@-1 {{redundant functional cast from '{{(rtl::)?}}OUString' to '{{(rtl::)?}}OUString' [loplugin:redundantfcast]}}
48 using T1 = OUString;
49 (void)T1(
50 s); // expected-error-re@-1 {{redundant functional cast from '{{(rtl::)?}}OUString' to 'T1' (aka '{{(rtl::)?}}OUString') [loplugin:redundantfcast]}}
51 using T2 = OUString const;
52 (void)T2(
53 s); // expected-error-re@-1 {{redundant functional cast from '{{(rtl::)?}}OUString' to 'T2' (aka 'const {{(rtl::?)}}OUString') [loplugin:redundantfcast]}}
55 (void)std::unique_ptr<int>(std::unique_ptr<int>(
56 new int{})); // expected-error@-1 {{redundant functional cast from 'std::unique_ptr<int>' to 'std::unique_ptr<int>' [loplugin:redundantfcast]}}
58 OUString s1;
59 method1(OUString(
60 s1)); // expected-error-re@-1 {{redundant functional cast from '{{(rtl::)?}}OUString' to '{{(rtl::)?}}OUString' [loplugin:redundantfcast]}}
61 OUString s2;
62 s2 = OUString(
63 s1); // expected-error-re@-1 {{redundant functional cast from '{{(rtl::)?}}OUString' to '{{(rtl::)?}}OUString' [loplugin:redundantfcast]}}
64 (void)s2;
66 Color col1;
67 Color col2 = Color(
68 col1); // expected-error@-1 {{redundant functional cast from 'Color' to 'Color' [loplugin:redundantfcast]}}
69 (void)col2;
71 Foo foo(1);
72 func1(Foo(
73 foo)); // expected-error@-1 {{redundant functional cast from 'Foo' to 'Foo' [loplugin:redundantfcast]}}
75 const tools::Polygon aPolygon;
76 ImplWritePolyPolygonRecord(tools::PolyPolygon(tools::Polygon(
77 aPolygon))); // expected-error@-1 {{redundant functional cast from 'const tools::Polygon' to 'tools::Polygon' in construct expression [loplugin:redundantfcast]}}
80 class Class1
82 Foo foo;
83 Foo func2()
85 return Foo(
86 foo); // expected-error@-1 {{redundant functional cast from 'Foo' to 'Foo' [loplugin:redundantfcast]}}
90 // casting of lambdas
91 namespace test5
93 void f1(std::function<void()> x);
94 void f2()
96 // expected-error-re@+1 {{redundant functional cast {{.+}} [loplugin:redundantfcast]}}
97 f1(std::function([&]() {}));
100 namespace test6
102 void f1(std::function<void(int)>);
103 void f1(std::function<void(long)>);
104 void f2()
106 f1(std::function<void(long)>([&](int) {})); // should not warn here
110 namespace test7
112 // expected-note@+1 6 {{in call to method here [loplugin:redundantfcast]}}
113 void f1(std::initializer_list<int> const&);
114 // expected-note@+1 6 {{in call to method here [loplugin:redundantfcast]}}
115 template <typename T> void f2(std::initializer_list<T> const&);
116 // expected-note@+1 4 {{in call to method here [loplugin:redundantfcast]}}
117 template <typename T> void f3(T const&);
118 // expected-note@+1 4 {{in call to method here [loplugin:redundantfcast]}}
119 template <typename... T> void f4(T const&...);
120 void f5(int, ...);
121 void g(std::initializer_list<int> il)
123 f1(il);
124 f2(il);
125 f3(il);
126 f4(il);
127 f5(0, il);
128 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
129 f1(std::initializer_list<int>(il));
130 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
131 f2(std::initializer_list<int>(il));
132 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
133 f3(std::initializer_list<int>(il));
134 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
135 f4(std::initializer_list<int>(il));
136 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
137 f5(0, std::initializer_list<int>(il));
138 f1({});
139 f1(std::initializer_list<int>{}); // should warn, but not modelled as CXXFunctionalCastExpr
140 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
141 f1(std::initializer_list<int>({}));
142 // f2({}); //error
143 f2(std::initializer_list<int>{}); // should warn, but not modelled as CXXFunctionalCastExpr
144 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
145 f2(std::initializer_list<int>({}));
146 // f3({}); //error
147 f3(std::initializer_list<int>{}); // (not modelled as CXXFunctionalCastExpr anyway)
148 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
149 f3(std::initializer_list<int>({})); // arguably rather subtle, remove "("...")"
150 // f4({}); //error
151 f4(std::initializer_list<int>{}); // (not modelled as CXXFunctionalCastExpr anyway)
152 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
153 f4(std::initializer_list<int>({})); // arguably rather subtle, remove "("...")"
154 // f5(0, {}); //error
155 f5(0, std::initializer_list<int>{}); // (not modelled as CXXFunctionalCastExpr anyway)
156 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
157 f5(0, std::initializer_list<int>({})); // arguably rather subtle, remove "("...")"
158 f1({ 1 });
159 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
160 f1(std::initializer_list<int>{ 1 });
161 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
162 f1(std::initializer_list<int>({ 1 }));
163 f2({ 1 });
164 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
165 f2(std::initializer_list<int>{ 1 });
166 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
167 f2(std::initializer_list<int>({ 1 }));
168 // f3({1}); //error
169 f3(std::initializer_list<int>{ 1 });
170 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
171 f3(std::initializer_list<int>({ 1 })); // arguably rather subtle, remove "("...")"
172 // f4({1}); //error
173 f4(std::initializer_list<int>{ 1 });
174 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
175 f4(std::initializer_list<int>({ 1 })); // arguably rather subtle, remove "("...")"
176 // f5(0, {1}); //error
177 f5(0, std::initializer_list<int>{ 1 });
178 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
179 f5(0, std::initializer_list<int>({ 1 })); // arguably rather subtle, remove "("...")"
180 f1({ 1, 2, 3 });
181 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
182 f1(std::initializer_list<int>{ 1, 2, 3 });
183 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
184 f1(std::initializer_list<int>({ 1, 2, 3 }));
185 f2({ 1, 2, 3 });
186 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
187 f2(std::initializer_list<int>{ 1, 2, 3 });
188 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
189 f2(std::initializer_list<int>({ 1, 2, 3 }));
190 // f3({1, 2, 3}); //error
191 f3(std::initializer_list<int>{ 1, 2, 3 });
192 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
193 f3(std::initializer_list<int>({ 1, 2, 3 })); // arguably rather subtle, remove "("...")"
194 // f4({1, 2, 3}); //error
195 f4(std::initializer_list<int>{ 1, 2, 3 });
196 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
197 f4(std::initializer_list<int>({ 1, 2, 3 })); // arguably rather subtle, remove "("...")"
198 // f5(0, {1, 2, 3}); //error
199 f5(0, std::initializer_list<int>{ 1, 2, 3 });
200 // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
201 f5(0, std::initializer_list<int>({ 1, 2, 3 })); // arguably rather subtle, remove "("...")"
205 namespace test8
207 class Primitive2DContainer
210 struct GroupPrimitive
212 GroupPrimitive(Primitive2DContainer&&);
215 const Primitive2DContainer& getChildren();
217 void foo()
219 // no warning expected, we have to create a temporary for this constructor
220 GroupPrimitive aGroup((Primitive2DContainer(getChildren())));
221 (void)aGroup;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */