Skip to content

Commit 6d61fb6

Browse files
authored
fixed #13765 - report unusedPrivateFunction on function implementation (#7449)
1 parent 5702fd2 commit 6d61fb6

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

lib/checkclass.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,16 +1325,19 @@ void CheckClass::privateFunctions()
13251325
}
13261326

13271327
if (!used)
1328-
unusedPrivateFunctionError(pf->tokenDef, scope->className, pf->name());
1328+
unusedPrivateFunctionError(pf->token, pf->tokenDef, scope->className, pf->name());
13291329

13301330
privateFuncs.pop_front();
13311331
}
13321332
}
13331333
}
13341334

1335-
void CheckClass::unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname)
1335+
void CheckClass::unusedPrivateFunctionError(const Token* tok1, const Token *tok2, const std::string &classname, const std::string &funcname)
13361336
{
1337-
reportError(tok, Severity::style, "unusedPrivateFunction", "$symbol:" + classname + "::" + funcname + "\nUnused private function: '$symbol'", CWE398, Certainty::normal);
1337+
std::list<const Token *> toks{ tok1 };
1338+
if (tok2)
1339+
toks.push_front(tok2);
1340+
reportError(toks, Severity::style, "unusedPrivateFunction", "$symbol:" + classname + "::" + funcname + "\nUnused private function: '$symbol'", CWE398, Certainty::normal);
13381341
}
13391342

13401343
//---------------------------------------------------------------------------
@@ -3796,7 +3799,7 @@ void CheckClass::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
37963799
c.uninitVarError(nullptr, true, FunctionType::eConstructor, "classname", "varnamepriv", true, false);
37973800
c.missingMemberCopyError(nullptr, FunctionType::eConstructor, "classname", "varnamepriv");
37983801
c.operatorEqVarError(nullptr, "classname", "", false);
3799-
c.unusedPrivateFunctionError(nullptr, "classname", "funcname");
3802+
c.unusedPrivateFunctionError(nullptr, nullptr, "classname", "funcname");
38003803
c.memsetError(nullptr, "memfunc", "classname", "class");
38013804
c.memsetErrorReference(nullptr, "memfunc", "class");
38023805
c.memsetErrorFloat(nullptr, "class");

lib/checkclass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class CPPCHECKLIB CheckClass : public Check {
159159
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname);
160160
void missingMemberCopyError(const Token *tok, FunctionType functionType, const std::string& classname, const std::string& varname);
161161
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive);
162-
void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname);
162+
void unusedPrivateFunctionError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname);
163163
void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type, bool isContainer = false);
164164
void memsetErrorReference(const Token *tok, const std::string &memfunc, const std::string &type);
165165
void memsetErrorFloat(const Token *tok, const std::string &type);

test/testunusedprivfunc.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class TestUnusedPrivateFunction : public TestFixture {
121121
"unsigned int Fred::f()\n"
122122
"{ }");
123123

124-
ASSERT_EQUALS("[test.cpp:4:18]: (style) Unused private function: 'Fred::f' [unusedPrivateFunction]\n", errout_str());
124+
ASSERT_EQUALS("[test.cpp:4:18] -> [test.cpp:12:20]: (style) Unused private function: 'Fred::f' [unusedPrivateFunction]\n", errout_str());
125125

126126
check("#line 1 \"p.h\"\n"
127127
"class Fred\n"
@@ -139,7 +139,7 @@ class TestUnusedPrivateFunction : public TestFixture {
139139
"unsigned int Fred::f()\n"
140140
"{ }");
141141

142-
ASSERT_EQUALS("[p.h:4:18]: (style) Unused private function: 'Fred::f' [unusedPrivateFunction]\n", errout_str());
142+
ASSERT_EQUALS("[p.h:4:18] -> [p.cpp:4:20]: (style) Unused private function: 'Fred::f' [unusedPrivateFunction]\n", errout_str());
143143

144144
check("#line 1 \"p.h\"\n"
145145
"class Fred\n"
@@ -154,7 +154,7 @@ class TestUnusedPrivateFunction : public TestFixture {
154154
"void Fred::f()\n"
155155
"{\n"
156156
"}");
157-
ASSERT_EQUALS("[p.h:4:6]: (style) Unused private function: 'Fred::f' [unusedPrivateFunction]\n", errout_str());
157+
ASSERT_EQUALS("[p.h:4:6] -> [p.cpp:2:12]: (style) Unused private function: 'Fred::f' [unusedPrivateFunction]\n", errout_str());
158158

159159
// Don't warn about include files which implementation we don't see
160160
check("#line 1 \"p.h\"\n"
@@ -274,7 +274,7 @@ class TestUnusedPrivateFunction : public TestFixture {
274274
"Fred::Fred()\n"
275275
"{}");
276276

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

280280

@@ -416,7 +416,7 @@ class TestUnusedPrivateFunction : public TestFixture {
416416
" static void f()\n"
417417
" { }\n"
418418
"};");
419-
ASSERT_EQUALS("[test.cpp:10:17]: (style) Unused private function: 'A::f' [unusedPrivateFunction]\n", errout_str());
419+
ASSERT_EQUALS("[test.cpp:10:17] -> [test.cpp:10:17]: (style) Unused private function: 'A::f' [unusedPrivateFunction]\n", errout_str());
420420

421421
check("class A\n"
422422
"{\n"
@@ -505,7 +505,7 @@ class TestUnusedPrivateFunction : public TestFixture {
505505
" void foo() {}\n" // Skip for overrides of virtual functions of base
506506
" void bar() {}\n" // Don't skip if no function is overridden
507507
"};");
508-
ASSERT_EQUALS("[test.cpp:9:10]: (style) Unused private function: 'derived::bar' [unusedPrivateFunction]\n", errout_str());
508+
ASSERT_EQUALS("[test.cpp:9:10] -> [test.cpp:9:10]: (style) Unused private function: 'derived::bar' [unusedPrivateFunction]\n", errout_str());
509509

510510
check("class Base {\n"
511511
"private:\n"
@@ -580,7 +580,7 @@ class TestUnusedPrivateFunction : public TestFixture {
580580
" friend Bar;\n"
581581
" void f() { }\n"
582582
"};");
583-
ASSERT_EQUALS("[test.cpp:5:10]: (style) Unused private function: 'Foo::f' [unusedPrivateFunction]\n", errout_str());
583+
ASSERT_EQUALS("[test.cpp:5:10] -> [test.cpp:5:10]: (style) Unused private function: 'Foo::f' [unusedPrivateFunction]\n", errout_str());
584584

585585
check("struct F;\n" // #10265
586586
"struct S {\n"
@@ -667,7 +667,7 @@ class TestUnusedPrivateFunction : public TestFixture {
667667
" void startListening() {\n"
668668
" }\n"
669669
"};");
670-
ASSERT_EQUALS("[test.cpp:8:10]: (style) Unused private function: 'Fred::startListening' [unusedPrivateFunction]\n", errout_str());
670+
ASSERT_EQUALS("[test.cpp:8:10] -> [test.cpp:8:10]: (style) Unused private function: 'Fred::startListening' [unusedPrivateFunction]\n", errout_str());
671671

672672
// #5059
673673
check("class Fred {\n"
@@ -836,7 +836,7 @@ class TestUnusedPrivateFunction : public TestFixture {
836836
"};\n"
837837
"int Foo::i = sth();"
838838
"int i = F();");
839-
ASSERT_EQUALS("[test.cpp:3:16]: (style) Unused private function: 'Foo::F' [unusedPrivateFunction]\n", errout_str());
839+
ASSERT_EQUALS("[test.cpp:3:16] -> [test.cpp:3:16]: (style) Unused private function: 'Foo::F' [unusedPrivateFunction]\n", errout_str());
840840
}
841841

842842
void templateSimplification() { //ticket #6183

0 commit comments

Comments
 (0)