File indexing completed on 2024-04-28 05:38:40

0001 #include <QtCore/QDebug>
0002 #include <QtGui/QColor>
0003 
0004 
0005 
0006 struct Trivial {
0007     int a;
0008 };
0009 
0010 struct NonTrivial {
0011     NonTrivial() {}
0012     NonTrivial(const NonTrivial &) {}
0013     void constFunction() const {};
0014     void nonConstFunction() {};
0015     int a;
0016 };
0017 
0018 extern void by_pointer(NonTrivial*);
0019 extern void by_const_pointer(const NonTrivial*);
0020 extern void by_ref(NonTrivial&);
0021 extern void by_constRef(const NonTrivial&);
0022 
0023 int foo1(const Trivial nt) // Test #1: No warning
0024 {
0025     return nt.a;
0026 }
0027 
0028 int foo2(const NonTrivial nt) // Test #2: Warning
0029 {
0030     nt.constFunction();
0031     return nt.a;
0032 }
0033 
0034 int foo3(NonTrivial nt) // Test #3: Warning
0035 {
0036     return nt.a;
0037 }
0038 
0039 int foo4(NonTrivial nt) // Test #4: Warning
0040 {
0041     return nt.a;
0042 }
0043 
0044 int foo5(NonTrivial nt) // Test #5: No warning
0045 {
0046     by_pointer(&nt);
0047     return nt.a;
0048 }
0049 
0050 
0051 int foo6(NonTrivial nt) // Test #6: No warning
0052 {
0053     by_ref(nt);
0054     return nt.a;
0055 }
0056 
0057 int foo7(NonTrivial nt) // Test #7: No warning
0058 {
0059     nt.nonConstFunction();
0060     return nt.a;
0061 }
0062 
0063 int foo8(NonTrivial nt) // Test #8: Warning
0064 {
0065     nt.constFunction();
0066     return nt.a;
0067 }
0068 
0069 int foo9(NonTrivial nt) // Test #9: Warning
0070 {
0071     by_constRef(nt);
0072     return nt.a;
0073 }
0074 
0075 int foo10(NonTrivial nt) // Test #10: Warning
0076 {
0077     by_const_pointer(&nt);
0078     return nt.a;
0079 }
0080 
0081 int foo11(QColor) // Test #11: No warning
0082 {
0083     return 1;
0084 }
0085 
0086 int foo12(NonTrivial nt) // Test #12: No warning
0087 {
0088     nt = NonTrivial();
0089     return 1;
0090 }
0091 
0092 void func(QString text)
0093 {
0094     QTextStream a(&text); // OK, by-pointer to a CTOR
0095 }
0096 
0097 struct StringConstPtr
0098 {
0099     StringConstPtr(const QString *s) {}
0100 };
0101 
0102 void func2(QString text)
0103 {
0104     StringConstPtr s(&text); // Warning, it's a const ptr
0105 }
0106 
0107 enum Option {
0108     FooOption,
0109     FooOption2
0110 };
0111 
0112 
0113 Q_DECLARE_FLAGS(Options, Option)
0114 Q_DECLARE_OPERATORS_FOR_FLAGS(Options)
0115 
0116 struct NoUserCtorsButNotTrivial
0117 {
0118     NonTrivial t;
0119 };
0120 
0121 void testNoUserCtorsButNotTrivial(NoUserCtorsButNotTrivial)
0122 {
0123 
0124 }
0125 
0126 struct TrivialWithDefaultCopyCtorAndDtor
0127 {
0128     TrivialWithDefaultCopyCtorAndDtor(const TrivialWithDefaultCopyCtorAndDtor &) = default;
0129     ~TrivialWithDefaultCopyCtorAndDtor() = default;
0130     int m;
0131 };
0132 
0133 void testTrivialWithDefaultCopyCtorAndDtor(TrivialWithDefaultCopyCtorAndDtor)
0134 {
0135 
0136 }
0137 
0138 struct DefaultButNotTrivialCopyable
0139 {
0140     DefaultButNotTrivialCopyable(const DefaultButNotTrivialCopyable & ) = default;
0141     ~DefaultButNotTrivialCopyable() = default;
0142     NonTrivial t;
0143 };
0144 
0145 void testDefaultButNotTrivialCopyable(DefaultButNotTrivialCopyable)
0146 {
0147 
0148 }
0149 
0150 void shouldBeByValue(const Trivial &nt)
0151 {
0152 
0153 }
0154 
0155 
0156 void isOK(const NonTrivial &&)
0157 {
0158 }
0159 
0160 void foo(const Trivial &t = Trivial());
0161 void foo1(const Trivial & = Trivial());
0162 void foo2(const Trivial &t);
0163 void foo3(const Trivial&);
0164 
0165 void foo4(const Trivial &t);
0166 void foo4(const Trivial &t)
0167 {
0168 }
0169 
0170 void foo5(const Trivial &t)
0171 {
0172 }
0173 
0174 void foo6(const Trivial &t = {})
0175 {
0176 }
0177 
0178 
0179 struct Base // Test that fixits fix both Base and Derived
0180 {
0181     void foo(const Trivial &);
0182 };
0183 
0184 void Base::foo(const Trivial &)
0185 {
0186 }
0187 
0188 struct Derived : public Base
0189 {
0190     void foo(const Trivial &);
0191 };
0192 
0193 void Derived::foo(const Trivial &)
0194 {
0195 }
0196 
0197 
0198 
0199 
0200 
0201 
0202 struct DeletedCtor // bug #360112
0203 {
0204     Q_DISABLE_COPY(DeletedCtor)
0205 };
0206 
0207 struct CopyCtor // bug #360112
0208 {
0209     CopyCtor(const CopyCtor &) {}
0210 };
0211 
0212 struct Ctors
0213 {
0214     Ctors (NonTrivial) {}
0215 };
0216 
0217 
0218 struct Ctors2
0219 {
0220     Ctors2(NonTrivial n) : m(std::move(n)), i(0) {} // Ok
0221     NonTrivial m;
0222     int i;
0223 };
0224 
0225 struct Ctors3
0226 {
0227     Ctors3(NonTrivial n) : m(n), i(0) {} // Warning
0228     NonTrivial m;
0229     int i;
0230 };
0231 
0232 
0233 // #381812
0234 class BaseWithVirtuals
0235 {
0236 public:
0237     virtual void virtualMethod1(NonTrivial) {}; // Warn
0238     virtual void virtualMethod2(NonTrivial) {}; // Warn
0239     void nonVirtualMethod(NonTrivial) {}; // Warn
0240 };
0241 
0242 class DerivedWithVirtuals : BaseWithVirtuals {
0243 public:
0244     void virtualMethod1(NonTrivial) override {}; // OK
0245     void virtualMethod2(NonTrivial) {}; // OK
0246     void nonVirtualMethod(NonTrivial) {}; // Warn
0247 };