Skip to content

Commit 13c4627

Browse files
unusedPrivateFunction: prevent redundant entries in error path (#8914)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent a80b7eb commit 13c4627

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

‎lib/checkclass.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,11 +1425,11 @@ void CheckClassImpl::privateFunctions()
14251425
}
14261426
}
14271427

1428-
void CheckClassImpl::unusedPrivateFunctionError(const Token* tok1, const Token *tok2, const std::string &classname, const std::string &funcname)
1428+
void CheckClassImpl::unusedPrivateFunctionError(const Token* tokImpl, const Token *tokDef, const std::string &classname, const std::string &funcname)
14291429
{
1430-
std::list<const Token *> toks{ tok1 };
1431-
if (tok2)
1432-
toks.push_front(tok2);
1430+
std::list<const Token *> toks{ tokDef };
1431+
if (tokDef != tokImpl)
1432+
toks.push_back(tokImpl);
14331433
reportError(toks, Severity::style, "unusedPrivateFunction", "$symbol:" + classname + "::" + funcname + "\nUnused private function: '$symbol'", CWE398, Certainty::normal);
14341434
}
14351435

‎lib/checkclass.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class CPPCHECKLIB CheckClassImpl : public CheckImpl {
186186
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname);
187187
void missingMemberCopyError(const Token *tok, FunctionType functionType, const std::string& classname, const std::string& varname);
188188
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive);
189-
void unusedPrivateFunctionError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname);
189+
void unusedPrivateFunctionError(const Token *tokImpl, const Token *tokDef, const std::string &classname, const std::string &funcname);
190190
void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type, bool isContainer = false);
191191
void memsetErrorReference(const Token *tok, const std::string &memfunc, const std::string &type);
192192
void memsetErrorFloat(const Token *tok, const std::string &type);

‎test/testunusedprivfunc.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class TestUnusedPrivateFunction : public TestFixture {
274274
"Fred::Fred()\n"
275275
"{}\n");
276276

277-
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:6:12]: (style) Unused private function: 'Fred::get' [unusedPrivateFunction]\n", errout_str());
277+
ASSERT_EQUALS("[test.cpp:6:12]: (style) Unused private function: 'Fred::get' [unusedPrivateFunction]\n", errout_str());
278278
}
279279

280280

@@ -407,7 +407,7 @@ class TestUnusedPrivateFunction : public TestFixture {
407407
" C() = default;\n"
408408
" void f() const { (void)this; }\n"
409409
"};\n");
410-
ASSERT_EQUALS("[test.cpp:3:10] -> [test.cpp:3:10]: (style) Unused private function: 'C::f' [unusedPrivateFunction]\n", errout_str());
410+
ASSERT_EQUALS("[test.cpp:3:10]: (style) Unused private function: 'C::f' [unusedPrivateFunction]\n", errout_str());
411411
}
412412

413413

@@ -424,7 +424,7 @@ class TestUnusedPrivateFunction : public TestFixture {
424424
" static void f()\n"
425425
" { }\n"
426426
"};\n");
427-
ASSERT_EQUALS("[test.cpp:10:17] -> [test.cpp:10:17]: (style) Unused private function: 'A::f' [unusedPrivateFunction]\n", errout_str());
427+
ASSERT_EQUALS("[test.cpp:10:17]: (style) Unused private function: 'A::f' [unusedPrivateFunction]\n", errout_str());
428428

429429
check("class A\n"
430430
"{\n"
@@ -460,7 +460,7 @@ class TestUnusedPrivateFunction : public TestFixture {
460460
" class I { I() = default; };\n"
461461
" void f() const { (void)this; }\n"
462462
"};\n");
463-
ASSERT_EQUALS("[test.cpp:3:10] -> [test.cpp:3:10]: (style) Unused private function: 'C::f' [unusedPrivateFunction]\n", errout_str());
463+
ASSERT_EQUALS("[test.cpp:3:10]: (style) Unused private function: 'C::f' [unusedPrivateFunction]\n", errout_str());
464464
}
465465

466466

@@ -501,7 +501,7 @@ class TestUnusedPrivateFunction : public TestFixture {
501501
" void foo() {}\n" // Skip for overrides of virtual functions of base
502502
" void bar() {}\n" // Don't skip if no function is overridden
503503
"};\n");
504-
ASSERT_EQUALS("[test.cpp:9:10] -> [test.cpp:9:10]: (style) Unused private function: 'derived::bar' [unusedPrivateFunction]\n", errout_str());
504+
ASSERT_EQUALS("[test.cpp:9:10]: (style) Unused private function: 'derived::bar' [unusedPrivateFunction]\n", errout_str());
505505

506506
check("class Base {\n"
507507
"private:\n"
@@ -576,7 +576,7 @@ class TestUnusedPrivateFunction : public TestFixture {
576576
" friend Bar;\n"
577577
" void f() { }\n"
578578
"};\n");
579-
ASSERT_EQUALS("[test.cpp:5:10] -> [test.cpp:5:10]: (style) Unused private function: 'Foo::f' [unusedPrivateFunction]\n", errout_str());
579+
ASSERT_EQUALS("[test.cpp:5:10]: (style) Unused private function: 'Foo::f' [unusedPrivateFunction]\n", errout_str());
580580

581581
check("struct F;\n" // #10265
582582
"struct S {\n"
@@ -663,7 +663,7 @@ class TestUnusedPrivateFunction : public TestFixture {
663663
" void startListening() {\n"
664664
" }\n"
665665
"};\n");
666-
ASSERT_EQUALS("[test.cpp:8:10] -> [test.cpp:8:10]: (style) Unused private function: 'Fred::startListening' [unusedPrivateFunction]\n", errout_str());
666+
ASSERT_EQUALS("[test.cpp:8:10]: (style) Unused private function: 'Fred::startListening' [unusedPrivateFunction]\n", errout_str());
667667

668668
// #5059
669669
check("class Fred {\n"
@@ -813,7 +813,7 @@ class TestUnusedPrivateFunction : public TestFixture {
813813
"};\n"
814814
"int Foo::i = sth();\n"
815815
"int i = F();\n");
816-
ASSERT_EQUALS("[test.cpp:3:16] -> [test.cpp:3:16]: (style) Unused private function: 'Foo::F' [unusedPrivateFunction]\n", errout_str());
816+
ASSERT_EQUALS("[test.cpp:3:16]: (style) Unused private function: 'Foo::F' [unusedPrivateFunction]\n", errout_str());
817817
}
818818

819819
void templateSimplification() { //ticket #6183

0 commit comments

Comments
 (0)