| 1 |
#include <QtCore/QDebug> |
| 2 |
#include <QtGui/QColor> |
| 3 |
#include <atomic> |
| 4 |
|
| 5 |
|
| 6 |
struct Trivial { |
| 7 |
int a; |
| 8 |
}; |
| 9 |
|
| 10 |
struct NonTrivial { |
| 11 |
NonTrivial() {} |
| 12 |
NonTrivial(const NonTrivial &) {} |
| 13 |
void constFunction() const {}; |
| 14 |
void nonConstFunction() {}; |
| 15 |
int a; |
| 16 |
}; |
| 17 |
|
| 18 |
void by_pointer(NonTrivial*) {} |
| 19 |
void by_const_pointer(const NonTrivial*) {} |
| 20 |
void by_ref(NonTrivial&) {} |
| 21 |
void by_constRef(const NonTrivial&) {} |
| 22 |
|
| 23 |
int foo1(const Trivial nt) // Test #1: No warning |
| 24 |
{ |
| 25 |
return nt.a; |
| 26 |
} |
| 27 |
|
| 28 |
int foo2(const NonTrivial nt) // Test #2: Warning |
| 29 |
{ |
| 30 |
nt.constFunction(); |
| 31 |
return nt.a; |
| 32 |
} |
| 33 |
|
| 34 |
int foo3(NonTrivial nt) // Test #3: Warning |
| 35 |
{ |
| 36 |
return nt.a; |
| 37 |
} |
| 38 |
|
| 39 |
int foo4(NonTrivial nt) // Test #4: Warning |
| 40 |
{ |
| 41 |
return nt.a; |
| 42 |
} |
| 43 |
|
| 44 |
int foo5(NonTrivial nt) // Test #5: No warning |
| 45 |
{ |
| 46 |
by_pointer(&nt); |
| 47 |
return nt.a; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
int foo6(NonTrivial nt) // Test #6: No warning |
| 52 |
{ |
| 53 |
by_ref(nt); |
| 54 |
return nt.a; |
| 55 |
} |
| 56 |
|
| 57 |
int foo7(NonTrivial nt) // Test #7: No warning |
| 58 |
{ |
| 59 |
nt.nonConstFunction(); |
| 60 |
return nt.a; |
| 61 |
} |
| 62 |
|
| 63 |
int foo8(NonTrivial nt) // Test #8: Warning |
| 64 |
{ |
| 65 |
nt.constFunction(); |
| 66 |
return nt.a; |
| 67 |
} |
| 68 |
|
| 69 |
int foo9(NonTrivial nt) // Test #9: Warning |
| 70 |
{ |
| 71 |
by_constRef(nt); |
| 72 |
return nt.a; |
| 73 |
} |
| 74 |
|
| 75 |
int foo10(NonTrivial nt) // Test #10: Warning |
| 76 |
{ |
| 77 |
by_const_pointer(&nt); |
| 78 |
return nt.a; |
| 79 |
} |
| 80 |
|
| 81 |
int foo11(QColor) // Test #11: No warning |
| 82 |
{ |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
int foo12(NonTrivial nt) // Test #12: No warning |
| 87 |
{ |
| 88 |
nt = NonTrivial(); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
void func(QString text) |
| 93 |
{ |
| 94 |
QTextStream a(&text); // OK, by-pointer to a CTOR |
| 95 |
} |
| 96 |
|
| 97 |
struct StringConstPtr |
| 98 |
{ |
| 99 |
StringConstPtr(const QString *s) {} |
| 100 |
}; |
| 101 |
|
| 102 |
void func2(QString text) |
| 103 |
{ |
| 104 |
StringConstPtr s(&text); // Warning, it's a const ptr |
| 105 |
} |
| 106 |
|
| 107 |
enum Option { |
| 108 |
FooOption, |
| 109 |
FooOption2 |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
Q_DECLARE_FLAGS(Options, Option) |
| 114 |
Q_DECLARE_OPERATORS_FOR_FLAGS(Options) |
| 115 |
|
| 116 |
struct NoUserCtorsButNotTrivial |
| 117 |
{ |
| 118 |
NonTrivial t; |
| 119 |
}; |
| 120 |
|
| 121 |
void testNoUserCtorsButNotTrivial(NoUserCtorsButNotTrivial) |
| 122 |
{ |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
struct TrivialWithDefaultCopyCtorAndDtor |
| 127 |
{ |
| 128 |
TrivialWithDefaultCopyCtorAndDtor(const TrivialWithDefaultCopyCtorAndDtor &) = default; |
| 129 |
~TrivialWithDefaultCopyCtorAndDtor() = default; |
| 130 |
int m; |
| 131 |
}; |
| 132 |
|
| 133 |
void testTrivialWithDefaultCopyCtorAndDtor(TrivialWithDefaultCopyCtorAndDtor) |
| 134 |
{ |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
struct DefaultButNotTrivialCopyable |
| 139 |
{ |
| 140 |
DefaultButNotTrivialCopyable(const DefaultButNotTrivialCopyable & ) = default; |
| 141 |
~DefaultButNotTrivialCopyable() = default; |
| 142 |
NonTrivial t; |
| 143 |
}; |
| 144 |
|
| 145 |
void testDefaultButNotTrivialCopyable(DefaultButNotTrivialCopyable) |
| 146 |
{ |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
void shouldBeByValue(Trivial nt) |
| 151 |
{ |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
void isOK(const NonTrivial &&) |
| 157 |
{ |
| 158 |
} |
| 159 |
|
| 160 |
void foo(const Trivial &t = Trivial()); |
| 161 |
void foo1(const Trivial & = Trivial()); |
| 162 |
void foo2(const Trivial &t); |
| 163 |
void foo3(const Trivial&); |
| 164 |
|
| 165 |
void foo4(Trivial t); |
| 166 |
void foo4(Trivial t) |
| 167 |
{ |
| 168 |
} |
| 169 |
|
| 170 |
void foo5(Trivial t) |
| 171 |
{ |
| 172 |
} |
| 173 |
|
| 174 |
void foo6(Trivial t = {}) |
| 175 |
{ |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
struct Base // Test that fixits fix both Base and Derived |
| 180 |
{ |
| 181 |
void foo(Trivial ); |
| 182 |
}; |
| 183 |
|
| 184 |
void Base::foo(Trivial ) |
| 185 |
{ |
| 186 |
} |
| 187 |
|
| 188 |
struct Derived : public Base |
| 189 |
{ |
| 190 |
void foo(Trivial ); |
| 191 |
}; |
| 192 |
|
| 193 |
void Derived::foo(Trivial ) |
| 194 |
{ |
| 195 |
} |
| 196 |
|
| 197 |
struct QDBusMessage |
| 198 |
{ |
| 199 |
void createErrorReply(QString) {} |
| 200 |
}; |
| 201 |
|
| 202 |
struct DeletedCtor // bug #360112 |
| 203 |
{ |
| 204 |
Q_DISABLE_COPY(DeletedCtor) |
| 205 |
}; |
| 206 |
|
| 207 |
struct CopyCtor // bug #360112 |
| 208 |
{ |
| 209 |
CopyCtor(const CopyCtor &) {} |
| 210 |
}; |
| 211 |
|
| 212 |
struct Ctors |
| 213 |
{ |
| 214 |
Ctors (NonTrivial) {} |
| 215 |
}; |
| 216 |
|
| 217 |
void trivialByConstRef(int t) {} // Warn |
| 218 |
void trivialByRef(int &t) {} // OK |
| 219 |
|
| 220 |
// #381812 |
| 221 |
|
| 222 |
class BaseWithVirtuals |
| 223 |
{ |
| 224 |
public: |
| 225 |
virtual void virtualMethod1(const Trivial &) {}; // Warn |
| 226 |
virtual void virtualMethod2(const Trivial &) {}; // Warn |
| 227 |
void nonVirtualMethod(Trivial ) {}; // Warn |
| 228 |
}; |
| 229 |
|
| 230 |
class DerivedWithVirtuals : BaseWithVirtuals { |
| 231 |
public: |
| 232 |
void virtualMethod1(const Trivial &) override {}; // OK |
| 233 |
void virtualMethod2(const Trivial &) {}; // OK |
| 234 |
void nonVirtualMethod(Trivial ) {}; // Warn |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
// bug #403088 |
| 239 |
void func(const std::atomic<int> &a) {} // Ok, since it's not trivially-copyable |
| 240 |
|
| 241 |
// generalization of bug #403088 |
| 242 |
struct DeletedCopyCtor { |
| 243 |
DeletedCopyCtor(const DeletedCopyCtor &) = delete; |
| 244 |
int v; |
| 245 |
}; |
| 246 |
|
| 247 |
void func(const DeletedCopyCtor &a) {} |