Debian Codegraph @

tests/range-loop-detach/main.cpp.fixed.expected

raw
1 #include <QtCore/QList>
2 #include <vector>
3 #include <QtCore/QMap>
4 #include <QtCore/QString>
5
6 void receivingList(QList<int>);
7 void receivingMap(QMultiMap<int,int>);
8
9 using namespace std;
10
11 QList<int> getQtList()
12 {
13 return {}; // dummy, not important
14 }
15
16 const QList<int> getConstQtList()
17 {
18 return {}; // dummy, not important
19 }
20
21 const QList<int> & getConstRefQtList()
22 {
23 static QList<int> foo;
24 return foo;
25 }
26
27 void testQtContainer()
28 {
29 QList<int> qt_container;
30 receivingList(qt_container);
31 for (int i : qAsConst(qt_container)) { // Warning
32 }
33
34 const QList<int> const_qt_container;
35 for (int i : const_qt_container) { // OK
36 }
37
38 for (int i : getQtList()) { // Warning
39 }
40
41 for (int i : qAsConst(qt_container)) { } // Warning
42 for (const int &i : qAsConst(qt_container)) { } // Warning
43 for (int &i : qt_container) { } // OK
44
45
46
47
48 for (int i : getConstQtList()) { // OK
49 }
50
51 for (int i : getConstRefQtList()) { // OK
52 }
53
54 vector<int> stl_container;
55 for (int i : stl_container) { // OK
56 }
57 }
58
59 class A {
60 public:
61 void foo()
62 {
63 for (int a : m_stlVec) {} // OK
64 }
65
66 std::vector<int> m_stlVec;
67 };
68
69 void testQMultiMapDetach()
70 {
71 QMultiMap<int,int> m;
72 receivingMap(m);
73 for (int i : qAsConst(m)) {
74 }
75 }
76
77 void testBug367485()
78 {
79 QList<int> list;
80 for (auto a : list) {} // OK
81
82 QList<int> list2;
83 receivingList(list2);
84 for (auto a : qAsConst(list2)) {} // Warning
85
86 QList<int> list3;
87 for (auto a : list3) {} // OK
88 receivingList(list3);
89
90 QList<int> list4;
91 foreach (auto a, list4) {} // OK
92 receivingList(list4);
93 }
94
95 struct SomeStruct
96 {
97 QStringList m_list;
98 void test_add_qasconst_fixits()
99 {
100 for (const auto &s : qAsConst(m_list)) {} // Warn
101 }
102
103 QStringList getList();
104 };
105
106
107 void test_add_qasconst_fixits()
108 {
109 SomeStruct f;
110 for (const auto &s : qAsConst(f.m_list)) {} // Warn
111
112 SomeStruct *f2;
113 for (const auto &s : qAsConst(f2->m_list)) {} // Warn
114
115 QStringList locallist = f.getList();
116 for (const auto &s : qAsConst(locallist)) {} // Warn
117
118 for (const auto &s : getQtList()) {} // Warn
119
120 for (const auto &s : f.getList()) {} // Warn
121 }