1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "sal/config.h"

#include "rtl/ustring.hxx"
#include "tools/color.hxx"

#include <functional>
#include <initializer_list>
#include <memory>

void method1(OUString const&); // expected-note {{in call to method here [loplugin:redundantfcast]}}

struct Foo
{
    Foo(int) {}
};

void func1(Foo const& f); // expected-note {{in call to method here [loplugin:redundantfcast]}}

namespace tools
{
struct Polygon
{
    Polygon() = default;
};
struct PolyPolygon
{
    PolyPolygon(
        Polygon const&); // expected-note {{in call to method here [loplugin:redundantfcast]}}
};
}

void ImplWritePolyPolygonRecord(const tools::PolyPolygon& rPolyPoly);

int main()
{
    OUString s;
    (void)OUString(
        s); // expected-error@-1 {{redundant functional cast from 'rtl::OUString' to 'rtl::OUString' [loplugin:redundantfcast]}}
    using T1 = OUString;
    (void)T1(
        s); // expected-error@-1 {{redundant functional cast from 'rtl::OUString' to 'T1' (aka 'rtl::OUString') [loplugin:redundantfcast]}}
    using T2 = OUString const;
    (void)T2(
        s); // expected-error@-1 {{redundant functional cast from 'rtl::OUString' to 'T2' (aka 'const rtl::OUString') [loplugin:redundantfcast]}}

    (void)std::unique_ptr<int>(std::unique_ptr<int>(
        new int{})); // expected-error@-1 {{redundant functional cast from 'std::unique_ptr<int>' to 'std::unique_ptr<int>' [loplugin:redundantfcast]}}

    OUString s1;
    method1(OUString(
        s1)); // expected-error@-1 {{redundant functional cast from 'rtl::OUString' to 'rtl::OUString' [loplugin:redundantfcast]}}
    OUString s2;
    s2 = OUString(<--- Variable 's2' is assigned a value that is never used.
        s1); // expected-error@-1 {{redundant functional cast from 'rtl::OUString' to 'rtl::OUString' [loplugin:redundantfcast]}}

    Color col1;
    Color col2 = Color(
        col1); // expected-error@-1 {{redundant functional cast from 'Color' to 'Color' [loplugin:redundantfcast]}}
    (void)col2;

    Foo foo(1);
    func1(Foo(
        foo)); // expected-error@-1 {{redundant functional cast from 'Foo' to 'Foo' [loplugin:redundantfcast]}}

    const tools::Polygon aPolygon;
    ImplWritePolyPolygonRecord(tools::PolyPolygon(tools::Polygon(
        aPolygon))); // expected-error@-1 {{redundant functional cast from 'const tools::Polygon' to 'tools::Polygon' [loplugin:redundantfcast]}}
}

class Class1
{
    Foo foo;
    Foo func2()<--- Unused private function: 'Class1::func2'
    {
        return Foo(
            foo); // expected-error@-1 {{redundant functional cast from 'Foo' to 'Foo' [loplugin:redundantfcast]}}
    }
};

// casting of lambdas
namespace test5
{
void f1(std::function<void()> x);
void f2()
{
    // expected-error-re@+1 {{redundant functional cast {{.+}} [loplugin:redundantfcast]}}
    f1(std::function([&]() {}));
}
};
namespace test6
{
void f1(std::function<void(int)>);
void f1(std::function<void(long)>);
void f2()
{
    f1(std::function<void(long)>([&](int) {})); // should not warn here
}
}

namespace test7
{
// expected-note@+1 6 {{in call to method here [loplugin:redundantfcast]}}
void f1(std::initializer_list<int> const&);
// expected-note@+1 6 {{in call to method here [loplugin:redundantfcast]}}
template <typename T> void f2(std::initializer_list<T> const&);
// expected-note@+1 4 {{in call to method here [loplugin:redundantfcast]}}
template <typename T> void f3(T const&);
// expected-note@+1 4 {{in call to method here [loplugin:redundantfcast]}}
template <typename... T> void f4(T const&...);
void f5(int, ...);
void g(std::initializer_list<int> il)
{
    f1(il);
    f2(il);
    f3(il);
    f4(il);
    f5(0, il);
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f1(std::initializer_list<int>(il));
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f2(std::initializer_list<int>(il));
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f3(std::initializer_list<int>(il));
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f4(std::initializer_list<int>(il));
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f5(0, std::initializer_list<int>(il));
    f1({});
    f1(std::initializer_list<int>{}); // should warn, but not modelled as CXXFunctionalCastExpr
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f1(std::initializer_list<int>({}));
    // f2({}); //error
    f2(std::initializer_list<int>{}); // should warn, but not modelled as CXXFunctionalCastExpr
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f2(std::initializer_list<int>({}));
    // f3({}); //error
    f3(std::initializer_list<int>{}); // (not modelled as CXXFunctionalCastExpr anyway)
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f3(std::initializer_list<int>({})); // arguably rather subtle, remove "("...")"
    // f4({}); //error
    f4(std::initializer_list<int>{}); // (not modelled as CXXFunctionalCastExpr anyway)
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f4(std::initializer_list<int>({})); // arguably rather subtle, remove "("...")"
    // f5(0, {}); //error
    f5(0, std::initializer_list<int>{}); // (not modelled as CXXFunctionalCastExpr anyway)
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f5(0, std::initializer_list<int>({})); // arguably rather subtle, remove "("...")"
    f1({ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f1(std::initializer_list<int>{ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f1(std::initializer_list<int>({ 1 }));
    f2({ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f2(std::initializer_list<int>{ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f2(std::initializer_list<int>({ 1 }));
    // f3({1}); //error
    f3(std::initializer_list<int>{ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f3(std::initializer_list<int>({ 1 })); // arguably rather subtle, remove "("...")"
    // f4({1}); //error
    f4(std::initializer_list<int>{ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f4(std::initializer_list<int>({ 1 })); // arguably rather subtle, remove "("...")"
    // f5(0, {1}); //error
    f5(0, std::initializer_list<int>{ 1 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f5(0, std::initializer_list<int>({ 1 })); // arguably rather subtle, remove "("...")"
    f1({ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f1(std::initializer_list<int>{ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f1(std::initializer_list<int>({ 1, 2, 3 }));
    f2({ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f2(std::initializer_list<int>{ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f2(std::initializer_list<int>({ 1, 2, 3 }));
    // f3({1, 2, 3}); //error
    f3(std::initializer_list<int>{ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f3(std::initializer_list<int>({ 1, 2, 3 })); // arguably rather subtle, remove "("...")"
    // f4({1, 2, 3}); //error
    f4(std::initializer_list<int>{ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f4(std::initializer_list<int>({ 1, 2, 3 })); // arguably rather subtle, remove "("...")"
    // f5(0, {1, 2, 3}); //error
    f5(0, std::initializer_list<int>{ 1, 2, 3 });
    // expected-error@+1 {{redundant functional cast from 'std::initializer_list<int>' to 'std::initializer_list<int>' [loplugin:redundantfcast]}}
    f5(0, std::initializer_list<int>({ 1, 2, 3 })); // arguably rather subtle, remove "("...")"
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */