Skip to content

Commit b10110b

Browse files
committed
CheckClass: Removed noExplicitCopyMoveConstructorError after discussion in http://sourceforge.net/p/cppcheck/discussion/general/thread/b2ce9d3d/.
1 parent ab90a7e commit b10110b

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

lib/checkclass.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ void CheckClass::checkExplicitConstructors()
241241
}
242242
}
243243

244-
// Abstract classes can't be instantiated. But if there is "misuse" by derived
245-
// classes then these constructors must be explicit.
244+
// Abstract classes can't be instantiated. But if there is C++11
245+
// "misuse" by derived classes then these constructors must be explicit.
246246
if (isAbstractClass && _settings->standards.cpp != Standards::CPP11)
247247
continue;
248248

@@ -256,13 +256,11 @@ void CheckClass::checkExplicitConstructors()
256256
if (!func->isConstructor() || func->isDelete() || (!func->hasBody() && func->access == Private))
257257
continue;
258258

259-
if (!func->isExplicit() && func->argCount() == 1) {
260-
// We must decide, if it is not a copy/move constructor, or it is a copy/move constructor of abstract class.
261-
if (func->type != Function::eCopyConstructor && func->type != Function::eMoveConstructor) {
262-
noExplicitConstructorError(func->tokenDef, scope->className, scope->type == Scope::eStruct);
263-
} else if (isAbstractClass) {
264-
noExplicitCopyMoveConstructorError(func->tokenDef, scope->className, scope->type == Scope::eStruct);
265-
}
259+
if (!func->isExplicit() &&
260+
func->argCount() == 1 &&
261+
func->type != Function::eCopyConstructor &&
262+
func->type != Function::eMoveConstructor) {
263+
noExplicitConstructorError(func->tokenDef, scope->className, scope->type == Scope::eStruct);
266264
}
267265
}
268266
}
@@ -788,13 +786,6 @@ void CheckClass::noExplicitConstructorError(const Token *tok, const std::string
788786
reportError(tok, Severity::style, "noExplicitConstructor", message + "\n" + verbose);
789787
}
790788

791-
void CheckClass::noExplicitCopyMoveConstructorError(const Token *tok, const std::string &classname, bool isStruct)
792-
{
793-
const std::string message(std::string(isStruct ? "Abstract struct" : "Abstract class") + " '" + classname + "' has a copy/move constructor that is not explicit.");
794-
const std::string verbose(message + " For abstract classes, even copy/move constructors may be declared explicit, as, by definition, abstract classes cannot be instantiated, and so objects of such type should never be passed by value.");
795-
reportError(tok, Severity::style, "noExplicitCopyMoveConstructor", message + "\n" + verbose);
796-
}
797-
798789
void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)
799790
{
800791
reportError(tok, Severity::warning, "uninitMemberVar", "Member variable '" + classname + "::" + varname + "' is not initialized in the constructor.", 0U, inconclusive);

lib/checkclass.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ class CPPCHECKLIB CheckClass : public Check {
142142
// Reporting errors..
143143
void noConstructorError(const Token *tok, const std::string &classname, bool isStruct);
144144
void noExplicitConstructorError(const Token *tok, const std::string &classname, bool isStruct);
145-
void noExplicitCopyMoveConstructorError(const Token *tok, const std::string &classname, bool isStruct);
146145
//void copyConstructorMallocError(const Token *cctor, const Token *alloc, const std::string& var_name);
147146
void copyConstructorShallowCopyError(const Token *tok, const std::string& varname);
148147
void noCopyConstructorError(const Token *tok, const std::string &classname, bool isStruct);
@@ -173,7 +172,6 @@ class CPPCHECKLIB CheckClass : public Check {
173172
CheckClass c(0, settings, errorLogger);
174173
c.noConstructorError(0, "classname", false);
175174
c.noExplicitConstructorError(0, "classname", false);
176-
c.noExplicitCopyMoveConstructorError(0, "classname", false);
177175
//c.copyConstructorMallocError(0, 0, "var");
178176
c.copyConstructorShallowCopyError(0, "var");
179177
c.noCopyConstructorError(0, "class", false);
@@ -208,7 +206,7 @@ class CPPCHECKLIB CheckClass : public Check {
208206
return "Check the code for each class.\n"
209207
"- Missing constructors and copy constructors\n"
210208
//"- Missing allocation of memory in copy constructor\n"
211-
"- Constructors which should be explicit are explicit\n"
209+
"- Constructors which should be explicit\n"
212210
"- Are all variables initialized by the constructors?\n"
213211
"- Are all variables assigned by 'operator='?\n"
214212
"- Warn if memset, memcpy etc are used on a class\n"

test/testclass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ class TestClass : public TestFixture {
237237
" Class(const Class& other) { }\n"
238238
" virtual int i() = 0;\n"
239239
"};");
240-
ASSERT_EQUALS("[test.cpp:2]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
240+
ASSERT_EQUALS("", errout.str());
241241

242242
checkExplicitConstructors("class Class {\n"
243243
" Class(Class&& other) { }\n"
244244
" virtual int i() = 0;\n"
245245
"};");
246-
ASSERT_EQUALS("[test.cpp:2]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
246+
ASSERT_EQUALS("", errout.str());
247247

248248
// #6585
249249
checkExplicitConstructors("class Class {\n"
@@ -256,7 +256,7 @@ class TestClass : public TestFixture {
256256
" public: Class(const Class&);\n"
257257
" virtual int i() = 0;\n"
258258
"};");
259-
ASSERT_EQUALS("[test.cpp:2]: (style) Abstract class 'Class' has a copy/move constructor that is not explicit.\n", errout.str());
259+
ASSERT_EQUALS("", errout.str());
260260
}
261261

262262
void checkDuplInheritedMembers(const char code[]) {

0 commit comments

Comments
 (0)