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
|
// clang+++ test.cpp -I /usr/include/qt/ -fPIC -lQt5Core -c
#include <vector>
#include <QtCore/QList>
#include <QtCore/QStringList>
#include <QtCore/QMap>
#include <QtGui/QRegion>
#include <QtCore/QVector>
#include <QtCore/QSequentialIterable>
void test_detachment()
{
// Test #1: Detaching the foreach container
QList<int> list;
foreach (int i, list) {
list.first();
}
}
struct Trivial
{
int a;
};
Q_DECLARE_TYPEINFO(Trivial, Q_PRIMITIVE_TYPE);
struct BigTrivial
{
int a, b, c, d, e;
void constFoo() const {}
void nonConstFoo() {}
};
struct SmallNonTrivial
{
int a;
~SmallNonTrivial() {}
};
extern void nop();
extern void nop2(BigTrivial &); // non-const-ref
extern void nop3(const BigTrivial &); // const-ref
extern void nop4(BigTrivial *); // pointer
void test_missing_ref()
{
QList<Trivial> trivials;
QList<BigTrivial> bigTrivials;
QList<SmallNonTrivial> smallNonTrivials;
// Test #2: No warning
foreach (Trivial t, trivials) {
nop();
}
// Test #3: Warning
foreach (BigTrivial t, bigTrivials) {
nop();
}
// Test #4: Warning
foreach (SmallNonTrivial t, smallNonTrivials) {
nop();
}
// Test #5: Warning
foreach (const BigTrivial t, bigTrivials) {
t.constFoo();
}
// Test #6: No warning
foreach (BigTrivial t, bigTrivials) {
t.nonConstFoo();
}
// Test #7: No warning
foreach (BigTrivial t, bigTrivials) {
t = BigTrivial();
}
// Test #8: No warning
foreach (BigTrivial t, bigTrivials) {
nop2(t);
}
// Test #9: Warning
foreach (BigTrivial t, bigTrivials) {
nop3(t);
}
// Test #9: No warning
foreach (BigTrivial t, bigTrivials) {
nop4(&t);
}
}
void testSTLForeach()
{
std::vector<int> v = {1, 2, 3, 4};
foreach (int i, v) { // Warning
}
}
void testQStringList()
{
QStringList sl;
sl << QChar('A') << QChar('B');
foreach (const QString &s, sl) { // no warning
}
}
void testQMultiMapDetach()
{
QMultiMap<int,int> m;
foreach (int i, m) {
m.first();
}
}
void testQRegionRects()
{
QRegion r;
foreach (const QRect &rect, r.rects()) {}
}
using Foo = QVarLengthArray<QString>;
void varLengthArray()
{
QVarLengthArray<int, 1> varray;
foreach (auto i, varray) {}
{
Foo foo;
Q_FOREACH(auto &&s, foo) {}
}
{
const Foo foo;
Q_FOREACH(auto &&s, foo) {}
}
}
void testQSequentialIterable()
{
QVariant vlist;
QSequentialIterable iterable = vlist.value<QSequentialIterable>();
foreach (const QVariant &v, iterable) {}
}
void testMemberQList()
{
struct { QStringList list; } data;
foreach (const QString &s, data.list) {};
}
|