diff --git a/tests/range-loop/main.cpp b/tests/range-loop/main.cpp index 3952003..b5980b3 100644 --- a/tests/range-loop/main.cpp +++ b/tests/range-loop/main.cpp @@ -1,239 +1,246 @@ #include #include #include #include #include #include - +#include void receivingList(QList); void receivingMap(QMultiMap); using namespace std; QList getQtList() { return {}; // dummy, not important } const QList getConstQtList() { return {}; // dummy, not important } const QList & getConstRefQtList() { static QList foo; return foo; } void testQtContainer() { QList qt_container; receivingList(qt_container); for (int i : qt_container) { // Warning } const QList const_qt_container; for (int i : const_qt_container) { // OK } for (int i : getQtList()) { // Warning } for (int i : qt_container) { } // Warning for (const int &i : qt_container) { } // Warning for (int &i : qt_container) { } // OK for (int i : getConstQtList()) { // OK } for (int i : getConstRefQtList()) { // OK } vector stl_container; for (int i : stl_container) { // OK } } class A { public: void foo() { for (int a : m_stlVec) {} // OK } std::vector m_stlVec; }; void testQMultiMapDetach() { QMultiMap m; receivingMap(m); for (int i : m) { } } struct Trivial { int a; }; 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() { const QList trivials; const QList bigTrivials; const QList smallNonTrivials; // Test #2: No warning for (Trivial t : trivials) { nop(); } // Test #3: No warning for (BigTrivial t : bigTrivials) { nop(); } // Test #4: Warning for (SmallNonTrivial t : smallNonTrivials) { nop(); } // Test #5: No Warning for (const BigTrivial t : bigTrivials) { t.constFoo(); } // Test #6: No warning for (BigTrivial t : bigTrivials) { t.nonConstFoo(); } // Test #7: No warning for (BigTrivial t : bigTrivials) { t = BigTrivial(); } // Test #8: No warning for (BigTrivial t : bigTrivials) { nop2(t); } // Test #9: No Warning for (BigTrivial t : bigTrivials) { nop3(t); } // Test #9: No warning for (BigTrivial t : bigTrivials) { nop4(&t); } // Test #10: No warning (bug #362587) QSequentialIterable si = QVariant().value(); for (const auto &s : si) {} } void testBug367485() { QList list; for (auto a : list) {} // OK QList list2; receivingList(list2); for (auto a : list2) {} // Warning QList list3; for (auto a : list3) {} // OK receivingList(list3); QList list4; foreach (auto a, list4) {} // OK receivingList(list4); } struct Foo { int bar = 1; QString s; }; std::vector doSomething(const std::vector &fooVec) { std::vector ret; for (Foo foo : fooVec) // OK { foo.bar = 2; ret.push_back(foo); } return ret; } void test_add_ref_fixits() { QStringList strlist; for (const QString s : strlist) {} // should add & for (QString s : strlist) {} // should add const-& for (QString s : strlist) { // shouldn't warn s = s.toLower(); } for (QString s : strlist) { // shouldn't warn s.clear(); } } struct SomeStruct { QStringList m_list; void test_add_qasconst_fixits() { for (const auto &s : m_list) {} // Warn } QStringList getList(); }; void test_add_qasconst_fixits() { SomeStruct f; for (const auto &s : f.m_list) {} // Warn SomeStruct *f2; for (const auto &s : f2->m_list) {} // Warn QStringList locallist = f.getList(); for (const auto &s : locallist) {} // Warn for (const auto &s : getQtList()) {} // Warn for (const auto &s : f.getList()) {} // Warn } + + +void test_json_array() +{ + QJsonArray array; + for (const auto a : array) {} // OK +} diff --git a/tests/range-loop/main.cpp.fixed.expected b/tests/range-loop/main.cpp.fixed.expected index 09e9a25..b8bc368 100644 --- a/tests/range-loop/main.cpp.fixed.expected +++ b/tests/range-loop/main.cpp.fixed.expected @@ -1,239 +1,246 @@ #include #include #include #include #include #include - +#include void receivingList(QList); void receivingMap(QMultiMap); using namespace std; QList getQtList() { return {}; // dummy, not important } const QList getConstQtList() { return {}; // dummy, not important } const QList & getConstRefQtList() { static QList foo; return foo; } void testQtContainer() { QList qt_container; receivingList(qt_container); for (int i : qAsConst(qt_container)) { // Warning } const QList const_qt_container; for (int i : const_qt_container) { // OK } for (int i : getQtList()) { // Warning } for (int i : qAsConst(qt_container)) { } // Warning for (const int &i : qAsConst(qt_container)) { } // Warning for (int &i : qt_container) { } // OK for (int i : getConstQtList()) { // OK } for (int i : getConstRefQtList()) { // OK } vector stl_container; for (int i : stl_container) { // OK } } class A { public: void foo() { for (int a : m_stlVec) {} // OK } std::vector m_stlVec; }; void testQMultiMapDetach() { QMultiMap m; receivingMap(m); for (int i : qAsConst(m)) { } } struct Trivial { int a; }; 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() { const QList trivials; const QList bigTrivials; const QList smallNonTrivials; // Test #2: No warning for (Trivial t : trivials) { nop(); } // Test #3: No warning for (BigTrivial t : bigTrivials) { nop(); } // Test #4: Warning for (const SmallNonTrivial &t : smallNonTrivials) { nop(); } // Test #5: No Warning for (const BigTrivial t : bigTrivials) { t.constFoo(); } // Test #6: No warning for (BigTrivial t : bigTrivials) { t.nonConstFoo(); } // Test #7: No warning for (BigTrivial t : bigTrivials) { t = BigTrivial(); } // Test #8: No warning for (BigTrivial t : bigTrivials) { nop2(t); } // Test #9: No Warning for (BigTrivial t : bigTrivials) { nop3(t); } // Test #9: No warning for (BigTrivial t : bigTrivials) { nop4(&t); } // Test #10: No warning (bug #362587) QSequentialIterable si = QVariant().value(); for (const auto &s : si) {} } void testBug367485() { QList list; for (auto a : list) {} // OK QList list2; receivingList(list2); for (auto a : qAsConst(list2)) {} // Warning QList list3; for (auto a : list3) {} // OK receivingList(list3); QList list4; foreach (auto a, list4) {} // OK receivingList(list4); } struct Foo { int bar = 1; QString s; }; std::vector doSomething(const std::vector &fooVec) { std::vector ret; for (Foo foo : fooVec) // OK { foo.bar = 2; ret.push_back(foo); } return ret; } void test_add_ref_fixits() { QStringList strlist; for (const QString &s : strlist) {} // should add & for (const QString &s : strlist) {} // should add const-& for (QString s : strlist) { // shouldn't warn s = s.toLower(); } for (QString s : strlist) { // shouldn't warn s.clear(); } } struct SomeStruct { QStringList m_list; void test_add_qasconst_fixits() { for (const auto &s : qAsConst(m_list)) {} // Warn } QStringList getList(); }; void test_add_qasconst_fixits() { SomeStruct f; for (const auto &s : qAsConst(f.m_list)) {} // Warn SomeStruct *f2; for (const auto &s : qAsConst(f2->m_list)) {} // Warn QStringList locallist = f.getList(); for (const auto &s : locallist) {} // Warn for (const auto &s : getQtList()) {} // Warn for (const auto &s : f.getList()) {} // Warn } + + +void test_json_array() +{ + QJsonArray array; + for (const auto a : array) {} +}