diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index fb8c7c8a19b..327830fb74e 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3082,15 +3082,18 @@ void CheckClass::checkUselessOverride() for (const Function& func : classScope->functionList) { if (!func.functionScope) continue; + if (func.hasFinalSpecifier()) + continue; const Function* baseFunc = func.getOverriddenFunction(); - if (!baseFunc || baseFunc->isPure()) + if (!baseFunc || baseFunc->isPure() || baseFunc->access != func.access) continue; if (const Token* const call = getSingleFunctionCall(func.functionScope)) { if (call->function() != baseFunc) continue; std::vector funcArgs = getArguments(func.tokenDef); std::vector callArgs = getArguments(call); - if (!std::equal(funcArgs.begin(), funcArgs.end(), callArgs.begin(), [](const Token* t1, const Token* t2) { + if (funcArgs.size() != callArgs.size() || + !std::equal(funcArgs.begin(), funcArgs.end(), callArgs.begin(), [](const Token* t1, const Token* t2) { return t1->str() == t2->str(); })) continue; diff --git a/test/testclass.cpp b/test/testclass.cpp index 40a61e81ec4..24e039faaeb 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -8403,6 +8403,32 @@ class TestClass : public TestFixture { " int f() override { return B::f(); }\n" "};"); ASSERT_EQUALS("", errout.str()); + + checkUselessOverride("struct S { virtual void f(); };\n" + "struct D : S {\n" + " void f() final { S::f(); }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + checkUselessOverride("struct S {\n" + "protected:\n" + " virtual void f();\n" + "};\n" + "struct D : S {\n" + "public:\n" + " void f() override { S::f(); }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + checkUselessOverride("struct B { virtual void f(int, int, int) const; };\n" + "struct D : B {\n" + " int m = 42;\n" + " void f(int a, int b, int c) const override;\n" + "};\n" + "void D::f(int a, int b, int c) const {\n" + " B::f(a, b, m);\n" + "};"); + ASSERT_EQUALS("", errout.str()); } #define checkUnsafeClassRefMember(code) checkUnsafeClassRefMember_(code, __FILE__, __LINE__)