| 1 |
#include <QtCore/QString> |
| 2 |
#include <QtCore/QList> |
| 3 |
#include <QtCore/QVector> |
| 4 |
#include <QtCore/QByteArray> |
| 5 |
#include <QtCore/QRect> |
| 6 |
#include <QtCore/QJsonValue> |
| 7 |
#include "other.h" |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
extern void external(QString); |
| 13 |
|
| 14 |
QString test() |
| 15 |
{ |
| 16 |
QString s; // Warning |
| 17 |
[[maybe_unused]] QString s_unused; // OK - has attribute |
| 18 |
|
| 19 |
QString s1, s2; // Warning for s2 |
| 20 |
[[maybe_unused]] QString s3_unused, s4_unused; // OK - both have attribute |
| 21 |
|
| 22 |
QString s3; // OK |
| 23 |
external(s1); |
| 24 |
|
| 25 |
return s3; |
| 26 |
return {}; |
| 27 |
} |
| 28 |
|
| 29 |
struct MyRAII |
| 30 |
{ |
| 31 |
MyRAII(); |
| 32 |
~MyRAII(); |
| 33 |
}; |
| 34 |
|
| 35 |
void testRAII() |
| 36 |
{ |
| 37 |
MyRAII m; // OK, not whitelisted |
| 38 |
[[maybe_unused]] MyRAII m_unused; // OK - has attribute |
| 39 |
} |
| 40 |
|
| 41 |
void testFor() |
| 42 |
{ |
| 43 |
QStringList l; |
| 44 |
for (QString s : l) // OK |
| 45 |
s; |
| 46 |
for ([[maybe_unused]] QString s : l) // OK - has attribute |
| 47 |
; |
| 48 |
foreach (QString s, l) // OK |
| 49 |
s; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void test4() |
| 54 |
{ |
| 55 |
QList<int> l; //Warn |
| 56 |
[[maybe_unused]] QList<int> l_unused; // OK - has attribute |
| 57 |
|
| 58 |
QVector<int> v; //Warn |
| 59 |
[[maybe_unused]] QVector<int> v_unused; // OK - has attribute |
| 60 |
|
| 61 |
QByteArray b; //Warn |
| 62 |
[[maybe_unused]] QByteArray b_unused; // OK - has attribute |
| 63 |
|
| 64 |
QRect r; // Warn |
| 65 |
[[maybe_unused]] QRect r_unused; // OK - has attribute |
| 66 |
|
| 67 |
QJsonValue jsv; // Warn |
| 68 |
[[maybe_unused]] QJsonValue jsv_unused; // OK - has attribute |
| 69 |
|
| 70 |
FOO(QRect) r2; // OK |
| 71 |
r2.setX(0); |
| 72 |
} |
| 73 |
|
| 74 |
struct MyWhitelistedType |
| 75 |
{ |
| 76 |
MyWhitelistedType(); |
| 77 |
~MyWhitelistedType(); |
| 78 |
}; |
| 79 |
|
| 80 |
void testUserWhitelist() |
| 81 |
{ |
| 82 |
MyWhitelistedType m; // OK, whitelisted |
| 83 |
[[maybe_unused]] MyWhitelistedType m_unused; // OK - whitelisted and has attribute |
| 84 |
} |
| 85 |
|
| 86 |
// Additional test cases for [[maybe_unused]] |
| 87 |
void testMixedScenarios() |
| 88 |
{ |
| 89 |
[[maybe_unused]] QList<int> list1; // OK - has attribute |
| 90 |
QList<int> list2; // Warning |
| 91 |
|
| 92 |
[[maybe_unused]] QString str1("test"); // OK - has attribute even if could be used |
| 93 |
QString str2; // Warning |
| 94 |
|
| 95 |
QStringList items; |
| 96 |
for ([[maybe_unused]] const QString &item : items) // OK - has attribute |
| 97 |
{ |
| 98 |
// Do nothing with item |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void testNestedContainers() |
| 103 |
{ |
| 104 |
[[maybe_unused]] QList<QVector<int>> nested1; // OK - has attribute |
| 105 |
QList<QVector<int>> nested2; // Warning |
| 106 |
|
| 107 |
[[maybe_unused]] QVector<QString> vec1; // OK - has attribute |
| 108 |
QVector<QString> vec2; // Warning |
| 109 |
} |