File: main.cpp.fixed.expected

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 (121 lines) | stat: -rw-r--r-- 2,217 bytes parent folder | download | duplicates (3)
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
#include <QtCore/QList>
#include <vector>
#include <QtCore/QMap>
#include <QtCore/QString>

void receivingList(QList<int>);
void receivingMap(QMultiMap<int,int>);

using namespace std;

QList<int> getQtList()
{
    return {}; // dummy, not important
}

const QList<int> getConstQtList()
{
    return {}; // dummy, not important
}

const QList<int> & getConstRefQtList()
{
    static QList<int> foo;
    return foo;
}

void testQtContainer()
{
    QList<int> qt_container;
    receivingList(qt_container);
    for (int i : qAsConst(qt_container)) { // Warning
    }

    const QList<int> 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<int> stl_container;
    for (int i : stl_container) { // OK
    }
}

class A {
public:
    void foo()
    {
        for (int a : m_stlVec) {} // OK
    }

    std::vector<int> m_stlVec;
};

void testQMultiMapDetach()
{
    QMultiMap<int,int> m;
    receivingMap(m);
    for (int i : qAsConst(m)) {
    }
}

void testBug367485()
{
    QList<int> list;
    for (auto a : list) {} // OK

    QList<int> list2;
    receivingList(list2);
    for (auto a : qAsConst(list2)) {} // Warning

    QList<int> list3;
    for (auto a : list3) {} // OK
    receivingList(list3);

    QList<int> list4;
    foreach (auto a, list4) {} // OK
    receivingList(list4);
}

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 : qAsConst(locallist)) {} // Warn

    for (const auto &s : getQtList()) {} // Warn

    for (const auto &s : f.getList()) {} // Warn
}