From f6726b76aec990bc862792b59cfd84774ab7d639 Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Thu, 1 Aug 2019 10:43:45 +0200 Subject: [PATCH] Fix 9215 (add tilde for missing destructor override warning) (#2057) --- lib/checkclass.cpp | 9 +++++---- test/testclass.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 7ec38bca7..d787429c5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2648,17 +2648,18 @@ void CheckClass::checkOverride() void CheckClass::overrideError(const Function *funcInBase, const Function *funcInDerived) { - const std::string functionName = funcInDerived ? funcInDerived->name() : ""; + const std::string functionName = funcInDerived ? ((funcInDerived->isDestructor() ? "~" : "") + funcInDerived->name()) : ""; + const std::string funcType = (funcInDerived && funcInDerived->isDestructor()) ? "destructor" : "function"; ErrorPath errorPath; if (funcInBase && funcInDerived) { - errorPath.push_back(ErrorPathItem(funcInBase->tokenDef, "Virtual function in base class")); - errorPath.push_back(ErrorPathItem(funcInDerived->tokenDef, "Function in derived class")); + errorPath.push_back(ErrorPathItem(funcInBase->tokenDef, "Virtual " + funcType + " in base class")); + errorPath.push_back(ErrorPathItem(funcInDerived->tokenDef, char(std::toupper(funcType[0])) + funcType.substr(1) + " in derived class")); } reportError(errorPath, Severity::style, "missingOverride", "$symbol:" + functionName + "\n" - "The function '$symbol' overrides a function in a base class but is not marked with a 'override' specifier.", + "The " + funcType + " '$symbol' overrides a " + funcType + " in a base class but is not marked with a 'override' specifier.", CWE(0U) /* Unknown CWE! */, false); } diff --git a/test/testclass.cpp b/test/testclass.cpp index 6bfa611cb..07ab003ad 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -7128,6 +7128,19 @@ private: " auto bar( ) const -> size_t override { return 0; }\n" "};"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:8]: (style) The function 'foo' overrides a function in a base class but is not marked with a 'override' specifier.\n", errout.str()); + + checkOverride("namespace Test {\n" + " class C {\n" + " public:\n" + " virtual ~C();\n" + " };\n" + "}\n" + "class C : Test::C {\n" + "public:\n" + " ~C();\n" + "};"); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:9]: (style) The destructor '~C' overrides a destructor in a base class but is not marked with a 'override' specifier.\n", errout.str()); + } void overrideCVRefQualifiers() {