aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/cplusplus/examples/tidy_example.cpp
blob: 31a74bc2f397a258c8ea54ecdd173c72eb7bf378 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

// llvm-include-order
#include <cassert>
#include <numeric>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <csetjmp>

#include "tidy_example.h"

#define INCREMENT_TWO(x, y) (x)++; (y)++

namespace Foo {

void macro()
{
    bool do_increment = true;
    int a, b;

    // misc-multiple-statement-macro
    if (do_increment)
        INCREMENT_TWO(a, b);
}

} // llvm-namespace-comment

// misc-forward-declaration-namespace
namespace na { struct A; }
namespace nb { struct A {}; }
nb::A a;

// cppcoreguidelines-special-member-functions
class Base
{
public:
    Base() {}

    // google-explicit-constructor
    Base(int arg) {}
    virtual ~Base() = default;
    Base(const Base &) = default;
    Base(Base &&) = default;

    // cppcoreguidelines-c-copy-assignment-signature
    // misc-noexcept-move-constructor
    // misc-unconventional-assign-operator
    // misc-unused-parameters
    Base operator=(Base &&param) { return {}; }
    virtual int function()
    {
        // modernize-use-nullptr
        int *a = 0;
        return 0;
    }
protected:
    void anotherFunctions(bool flag);
public:
    // cppcoreguidelines-pro-type-member-init
    int value;
};

// cert-err58-cpp
static Base base;

// modernize-redundant-void-arg
double getDouble(void)
{
    return 10.5;
}

void bad_malloc(char *str)
{
    // modernize-use-auto
    // cppcoreguidelines-pro-type-cstyle-cast
    // google-readability-casting
    // cppcoreguidelines-no-malloc
    // cppcoreguidelines-pro-bounds-pointer-arithmetic
    char *c = (char*) malloc(strlen(str + 1));
}

template <typename T>
void foo(T&& t)
{
    // misc-move-forwarding-reference
    bar(std::move(t));
}

void afterMove(Base &&base)
{
    Base moved(std::move(base));

    // misc-use-after-move
    (void) base.value;
}

// google-runtime-references
void reference(Base &base)
{

}

class Derived : public Base {
public:
    // modernize-use-equals-default
    Derived() {}

    // readability-named-parameter
    Derived(const Derived &) {}

    // modernize-use-override
    int function()
    {
        int a = 0;
        bool *p = nullptr;
        // misc-bool-pointer-implicit-conversion, readability-implicit-bool-cast
        if (p) {
        }
        auto b = {0.5f, 0.5f, 0.5f, 0.5f};

        // misc-fold-init-type
        (void) std::accumulate(std::begin(b), std::end(b), 0);

        // google-readability-casting, misc-incorrect-roundings
        auto c = (int)(getDouble() + 0.5);

        // misc-string-constructor
        std::string str('x', 50);

        int i[5] = {1, 2, 3, 4, 5};
        // cppcoreguidelines-pro-bounds-array-to-pointer-decay
        int *ip = i;

        // cert-flp30-c
        for (float a = .0; a != 5.; a += .1) {
            // cert-msc30-c, cert-msc50-cpp
            std::rand();
        }

        // misc-sizeof-container
        const Base constVal = sizeof(str);

        // cppcoreguidelines-pro-type-const-cast
        auto val = const_cast<Base &>(constVal);

        // readability-redundant-string-init
        std::string str2 = "";

        // misc-string-compare
        if (str.compare(str2)) {
            return 0;
        }

        // cppcoreguidelines-pro-type-reinterpret-cast
        return a + c + reinterpret_cast<int64_t>(&val);
    }
};

union Union
{
    int intVal;
    double doubleVal;
};

class Derived2 : public Base
{
public:
    // misc-virtual-near-miss
    virtual int functiom()
    {
        // cert-env33-c
        std::system("echo ");
        // cert-err52-cpp
        setjmp(nullptr);
        return 0;
    }

    // google-default-arguments
    virtual bool check(bool enable = true);
};

// performance-unnecessary-value-param
void use(Base b)
{

}

void FancyFunction()
{
    // cppcoreguidelines-pro-type-vararg
    // cppcoreguidelines-pro-bounds-array-to-pointer-decay
    // misc-lambda-function-name
    [] { printf("Called from %s\n", __func__); }();
}

// modernize-use-using
typedef int *int_ptr;

// readability-avoid-const-params-in-decls, misc-misplaced-const
void f(const int_ptr ptr);

void f(int *X, int Y)
{
    // readability-misplaced-array-index
    Y[X] = 0;
}

int main()
{
    // cppcoreguidelines-pro-type-member-init
    Union u;

    // cppcoreguidelines-pro-type-union-access
    u.doubleVal = 10.;

    // readability-braces-around-statements, readability-simplify-boolean-expr
    if (false)
        return 1;
    // readability-else-after-return
    else {
        // google-readability-todo
        // TODO: implement something
    }

    int arr[] = {1,2,3};

    // modernize-loop-convert
    for (int i = 0; i < 3; ++i) {
        // cppcoreguidelines-pro-bounds-constant-array-index
        (void) arr[i];
    }

    std::vector<std::pair<int, int>> w;

    // modernize-use-emplace
    w.push_back(std::pair<int, int>(21, 37));

    bool val = false;
    if (val)
        if (val)
            Foo::macro();
    // readability-misleading-indentation
    else
        Foo::macro();

    Derived derived;

    // cppcoreguidelines-slicing
    use(derived);
    return 0;
}