From f5044bb65f6cc4d8bd9fd15aa57ed1048de6d52b Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 1 Aug 2017 19:52:41 -0400 Subject: [PATCH] Fixed #8142 (false positive: The class 'B' has 'copy constructor' but lack of 'operator='.) Do not count static member variables when trying to figure out if 'operator =' is needed. --- lib/checkclass.cpp | 8 +++++++- test/testclass.cpp | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 3c0936d45..04d7cfbdb 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2383,7 +2383,13 @@ void CheckClass::checkCopyCtorAndEqOperator() for (std::size_t i = 0; i < classes; ++i) { const Scope * scope = symbolDatabase->classAndStructScopes[i]; - if (scope->varlist.empty()) + // count the number of non-static variables + int vars = 0; + for (std::list::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { + if (!var->isStatic()) + vars++; + } + if (vars == 0) continue; int hasCopyCtor = 0; diff --git a/test/testclass.cpp b/test/testclass.cpp index 8af201a47..2004f5ada 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -254,6 +254,26 @@ private: " int x;\n" "};"); ASSERT_EQUALS("", errout.str()); + + checkCopyCtorAndEqOperator("class A {\n" + "public:\n" + " A() : x(0) { }\n" + " A(const A & a) { x = a.x; }\n" + " A & operator = (const A & a) {\n" + " x = a.x;\n" + " return *this;\n" + " }\n" + "private:\n" + " int x;\n" + "};\n" + "class B : public A {\n" + "public:\n" + " B() { }\n" + " B(const B & b) :A(b) { }\n" + "private:\n" + " static int i;\n" + "};"); + ASSERT_EQUALS("", errout.str()); } void checkExplicitConstructors(const char code[]) {