Skip to content

Commit 94aafa4

Browse files
author
Pete Johns
committed
Fixed danmar#2480 (false positive on unused private function)
1 parent 856a631 commit 94aafa4

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

lib/checkclass.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,11 @@ void CheckClass::privateFunctions()
734734
// or if the function address is used somewhere...
735735
// eg. sigc::mem_fun(this, &className::classFunction)
736736
const std::string _pattern2("& " + classname + " :: " + FuncList.front()->str());
737+
const std::string methodAsArgument("(|, " + classname + " :: " + FuncList.front()->str() + " ,|)");
737738
if (!Token::findmatch(_tokenizer->tokens(), _pattern.c_str()) &&
738-
!Token::findmatch(_tokenizer->tokens(), _pattern2.c_str()))
739+
!Token::findmatch(_tokenizer->tokens(), _pattern2.c_str()) &&
740+
!Token::findmatch(_tokenizer->tokens(), methodAsArgument.c_str())
741+
)
739742
{
740743
unusedPrivateFunctionError(FuncList.front(), classname, FuncList.front()->str());
741744
}

test/testunusedprivfunc.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class TestUnusedPrivateFunction : public TestFixture
6060

6161
// #2407 - FP when called from operator()
6262
TEST_CASE(fp_operator);
63-
TEST_CASE(testDoesNotIdentifyCallback); // #2480
63+
TEST_CASE(testDoesNotIdentifyMethodAsFirstFunctionArgument); // #2480
64+
TEST_CASE(testDoesNotIdentifyMethodAsMiddleFunctionArgument);
65+
TEST_CASE(testDoesNotIdentifyMethodAsLastFunctionArgument);
6466
}
6567

6668

@@ -449,7 +451,7 @@ class TestUnusedPrivateFunction : public TestFixture
449451
ASSERT_EQUALS("", errout.str());
450452
}
451453

452-
void testDoesNotIdentifyCallback()
454+
void testDoesNotIdentifyMethodAsFirstFunctionArgument()
453455
{
454456
check("#include <iostream>"
455457
"void callback(void (*func)(int), int arg)"
@@ -474,8 +476,66 @@ class TestUnusedPrivateFunction : public TestFixture
474476
"{"
475477
" MountOperation aExample(10);"
476478
"}"
477-
);
478-
TODO_ASSERT_EQUALS("", errout.str());
479+
);
480+
ASSERT_EQUALS("", errout.str());
481+
}
482+
483+
void testDoesNotIdentifyMethodAsMiddleFunctionArgument()
484+
{
485+
check("#include <iostream>"
486+
"void callback(char, void (*func)(int), int arg)"
487+
"{"
488+
" (*func)(arg);"
489+
"}"
490+
"class MountOperation"
491+
"{"
492+
" static void Completed(int i);"
493+
"public:"
494+
" MountOperation(int i);"
495+
"};"
496+
"void MountOperation::Completed(int i)"
497+
"{"
498+
" std::cerr << i << std::endl;"
499+
"}"
500+
"MountOperation::MountOperation(int i)"
501+
"{"
502+
" callback('a', MountOperation::Completed, i);"
503+
"}"
504+
"int main(void)"
505+
"{"
506+
" MountOperation aExample(10);"
507+
"}"
508+
);
509+
ASSERT_EQUALS("", errout.str());
510+
}
511+
512+
void testDoesNotIdentifyMethodAsLastFunctionArgument()
513+
{
514+
check("#include <iostream>"
515+
"void callback(int arg, void (*func)(int))"
516+
"{"
517+
" (*func)(arg);"
518+
"}"
519+
"class MountOperation"
520+
"{"
521+
" static void Completed(int i);"
522+
"public:"
523+
" MountOperation(int i);"
524+
"};"
525+
"void MountOperation::Completed(int i)"
526+
"{"
527+
" std::cerr << i << std::endl;"
528+
"}"
529+
"MountOperation::MountOperation(int i)"
530+
"{"
531+
" callback(i, MountOperation::Completed);"
532+
"}"
533+
"int main(void)"
534+
"{"
535+
" MountOperation aExample(10);"
536+
"}"
537+
);
538+
ASSERT_EQUALS("", errout.str());
479539
}
480540
};
481541

0 commit comments

Comments
 (0)