File indexing completed on 2024-11-17 05:06:03

0001 #include <QtCore/QObject>
0002 #include <QtCore/QDebug>
0003 struct A
0004 {
0005     int v;
0006 };
0007 
0008 
0009 void test()
0010 {
0011     QObject *o;
0012     int a, b, c;
0013     auto f = [&a]() {}; // OK
0014     o->connect(o, &QObject::destroyed, [a]() {}); // OK
0015     o->connect(o, &QObject::destroyed, [&a]() {}); // Warning
0016     QObject::connect(o, &QObject::destroyed, [&a]() { }); // Warning
0017     QObject::connect(o, &QObject::destroyed, [&]() { a; b; }); // Warning
0018     QObject::connect(o, &QObject::destroyed, [=]() { a; b; }); // OK
0019 
0020     A *a1;
0021     QObject::connect(o, &QObject::destroyed, [a1]() { a1->v; }); // OK
0022     QObject::connect(o, &QObject::destroyed, [&a1]() { a1->v; }); // Warning
0023 }
0024 
0025 
0026 static int s;
0027 
0028 struct C
0029 {
0030 
0031     void foo()
0032     {
0033         QObject *o;
0034         int m;
0035         QObject::connect(o, &QObject::destroyed, [this]() { }); // OK
0036         QObject::connect(o, &QObject::destroyed, []() { s; }); // OK
0037         QObject::connect(o, &QObject::destroyed, [&m]() { m; }); // Warn
0038 
0039         QObject o2;
0040         QObject::connect(&o2, &QObject::destroyed, [&m]() { m; }); // OK, o is on the stack
0041 
0042         QObject *o3;
0043         QObject::connect(o3, &QObject::destroyed,
0044                          o3, [&o3] { o3; }); // OK, the captured variable is on the 3rd parameter too. It will get destroyed
0045     }
0046 
0047     int m;
0048 };
0049 
0050 
0051 void bug3rdArgument(QObject *sender)
0052 {
0053     // Checks that we shouldn't warn when the captured variable matches the 3rd argument
0054 
0055     QObject context;
0056     int m = 0;
0057     QObject::connect(sender, &QObject::destroyed, &context, [&] {
0058         qDebug() << m; // Warn
0059         qDebug() << context.objectName(); // OK
0060     });
0061 }