Skip to content

Commit

Permalink
Fix FPs: uselessOverride (#5208)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jun 29, 2023
1 parent a40e581 commit e9feeef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Token*> funcArgs = getArguments(func.tokenDef);
std::vector<const Token*> 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;
Expand Down
26 changes: 26 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" // #11799
"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__)
Expand Down

0 comments on commit e9feeef

Please sign in to comment.