Support C++03-style of declaring a copy ctor as deleted (#6585)
This commit is contained in:
parent
e8ac4d8e87
commit
a3eb725c3f
|
@ -248,7 +248,7 @@ void CheckClass::checkExplicitConstructors()
|
||||||
// 2) Constructor is not declared as explicit
|
// 2) Constructor is not declared as explicit
|
||||||
// 3) It is not a copy/move constructor of non-abstract class
|
// 3) It is not a copy/move constructor of non-abstract class
|
||||||
// 4) Constructor is not marked as delete (programmer can mark the default constructor as deleted, which is ok)
|
// 4) Constructor is not marked as delete (programmer can mark the default constructor as deleted, which is ok)
|
||||||
if (!func->isConstructor() || func->isDelete())
|
if (!func->isConstructor() || func->isDelete() || (!func->hasBody() && func->access == Private))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!func->isExplicit() && func->argCount() == 1) {
|
if (!func->isExplicit() && func->argCount() == 1) {
|
||||||
|
|
|
@ -198,54 +198,61 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void explicitConstructors() {
|
void explicitConstructors() {
|
||||||
checkExplicitConstructors("class Class \n"
|
checkExplicitConstructors("class Class {\n"
|
||||||
"{ \n"
|
" Class() = delete;\n"
|
||||||
" Class() = delete; \n"
|
" Class(const Class& other) { }\n"
|
||||||
" Class(const Class& other) { } \n"
|
" Class(Class&& other) { }\n"
|
||||||
" Class(Class&& other) { } \n"
|
" explicit Class(int i) { }\n"
|
||||||
" explicit Class(int i) { } \n"
|
" explicit Class(const std::string&) { }\n"
|
||||||
" explicit Class(const std::string&) { } \n"
|
" Class(int a, int b) { }\n"
|
||||||
" Class(int a, int b) { } \n"
|
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
checkExplicitConstructors("class Class \n"
|
checkExplicitConstructors("class Class {\n"
|
||||||
"{ \n"
|
" Class() = delete;\n"
|
||||||
" Class() = delete; \n"
|
" explicit Class(const Class& other) { }\n"
|
||||||
" explicit Class(const Class& other) { } \n"
|
" explicit Class(Class&& other) { }\n"
|
||||||
" explicit Class(Class&& other) { } \n"
|
" virtual int i() = 0;\n"
|
||||||
" virtual int i() = 0; \n"
|
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
checkExplicitConstructors("class Class \n"
|
checkExplicitConstructors("class Class {\n"
|
||||||
"{ \n"
|
" Class() = delete;\n"
|
||||||
" Class() = delete; \n"
|
" Class(const Class& other) = delete;\n"
|
||||||
" Class(const Class& other) = delete; \n"
|
" Class(Class&& other) = delete;\n"
|
||||||
" Class(Class&& other) = delete; \n"
|
" virtual int i() = 0;\n"
|
||||||
" virtual int i() = 0; \n"
|
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
checkExplicitConstructors("class Class \n"
|
checkExplicitConstructors("class Class {\n"
|
||||||
"{ \n"
|
" Class(int i) { }\n"
|
||||||
" Class(int i) { } \n"
|
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Class 'Class' has a constructor with 1 argument that is not explicit.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (style) Class 'Class' has a constructor with 1 argument that is not explicit.\n", errout.str());
|
||||||
|
|
||||||
checkExplicitConstructors("class Class \n"
|
checkExplicitConstructors("class Class {\n"
|
||||||
"{ \n"
|
" Class(const Class& other) { }\n"
|
||||||
" Class(const Class& other) { } \n"
|
" virtual int i() = 0;\n"
|
||||||
" virtual int i() = 0; \n"
|
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
|
||||||
|
|
||||||
checkExplicitConstructors("class Class \n"
|
checkExplicitConstructors("class Class {\n"
|
||||||
"{ \n"
|
" Class(Class&& other) { }\n"
|
||||||
" Class(Class&& other) { } \n"
|
" virtual int i() = 0;\n"
|
||||||
" virtual int i() = 0; \n"
|
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
|
||||||
|
|
||||||
|
// #6585
|
||||||
|
checkExplicitConstructors("class Class {\n"
|
||||||
|
" private: Class(const Class&);\n"
|
||||||
|
" virtual int i() = 0;\n"
|
||||||
|
"};");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
checkExplicitConstructors("class Class {\n"
|
||||||
|
" public: Class(const Class&);\n"
|
||||||
|
" virtual int i() = 0;\n"
|
||||||
|
"};");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkDuplInheritedMembers(const char code[]) {
|
void checkDuplInheritedMembers(const char code[]) {
|
||||||
|
|
Loading…
Reference in New Issue