File: main.cpp

package info (click to toggle)
clazy 1.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,384 kB
  • sloc: cpp: 24,969; python: 1,429; xml: 448; sh: 237; makefile: 48
file content (64 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (2)
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
#include <QtCore/QObject>
#include <QtCore/QDebug>
struct A
{
    int v;
};


void test()
{
    QObject *o;
    int a, b, c;
    auto f = [&a]() {}; // OK
    o->connect(o, &QObject::destroyed, [a]() {}); // OK
    o->connect(o, &QObject::destroyed, [&a]() {}); // Warning
    QObject::connect(o, &QObject::destroyed, [&a]() { }); // Warning
    QObject::connect(o, &QObject::destroyed, [&]() { a; b; }); // Warning
    QObject::connect(o, &QObject::destroyed, [=]() { a; b; }); // OK

    A *a1;
    QObject::connect(o, &QObject::destroyed, [a1]() { a1->v; }); // OK
    QObject::connect(o, &QObject::destroyed, [&a1]() { a1->v; }); // Warning
}


static int s;

struct C
{

    void foo()
    {
        QObject *o;
        int m;
        QObject::connect(o, &QObject::destroyed, [this]() { }); // OK
        QObject::connect(o, &QObject::destroyed, []() { s; }); // OK
        QObject::connect(o, &QObject::destroyed, [&m]() { m; }); // Warn

        QObject o2;
        QObject::connect(&o2, &QObject::destroyed, [&m]() { m; }); // OK, o2 is on the stack

        QObject *o3;
        QObject::connect(o3, &QObject::destroyed,
                         o3, [&o3] { o3; }); // OK, the captured variable is on the 3rd parameter too. It will get destroyed

        QObject::connect(o3, &QObject::destroyed, &o2, [&m]() { m; }); // OK, o2 is on the stack
    }
};


void bug3rdArgument(QObject *sender)
{
    // Checks that we shouldn't warn when the captured variable matches the 3rd argument

    QObject context;
    QObject *obj;
    int m = 0;
    QObject::connect(sender, &QObject::destroyed, obj, [&] {
        qDebug() << m; // Warn
    });
    QObject::connect(sender, &QObject::destroyed, &context, [&] {
        qDebug() << context.objectName(); // OK
    });
}