Skip to content

Commit 379807a

Browse files
committed
Fixed TODO_ASSERT_EQUAL and cppcheck-opensource#5614 caused by bad simplification of return values.
1 parent 2248cdf commit 379807a

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5107,7 +5107,7 @@ bool Tokenizer::simplifyFunctionReturn()
51075107
if (tok->str() == "{")
51085108
tok = tok->link();
51095109

5110-
else if (Token::Match(tok, "%var% ( ) { return %bool%|%char%|%num%|%str% ; }")) {
5110+
else if (Token::Match(tok, "%var% ( ) { return %bool%|%char%|%num%|%str% ; }") && tok->strAt(-1) != "::") {
51115111
const Token* const any = tok->tokAt(5);
51125112

51135113
const std::string pattern("(|[|=|%cop% " + tok->str() + " ( ) ;|]|)|%cop%");

test/testclass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,8 +2812,8 @@ class TestClass : public TestFixture {
28122812
" std::string s;\n"
28132813
" const std::string & foo();\n"
28142814
"};\n"
2815-
"const std::string & Fred::foo() { return \"\"; }", 0, false, false);
2816-
TODO_ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (performance, inconclusive) Technically the member function 'Fred::foo' can be static.\n", "", errout.str());
2815+
"const std::string & Fred::foo() { return \"\"; }");
2816+
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (performance, inconclusive) Technically the member function 'Fred::foo' can be static.\n", errout.str());
28172817

28182818
// functions with a function call to a non-const member can't be const.. (#1305)
28192819
checkConst("class Fred\n"

test/testsimplifytokens.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7664,22 +7664,37 @@ class TestSimplifyTokens : public TestFixture {
76647664
}
76657665

76667666
void simplifyFunctionReturn() {
7667-
const char code[] = "typedef void (*testfp)();\n"
7668-
"struct Fred\n"
7669-
"{\n"
7670-
" testfp get1() { return 0; }\n"
7671-
" void ( * get2 ( ) ) ( ) { return 0 ; }\n"
7672-
" testfp get3();\n"
7673-
" void ( * get4 ( ) ) ( );\n"
7674-
"};";
7675-
const char expected[] = "struct Fred "
7676-
"{ "
7677-
"void ( * get1 ( ) ) ( ) { return 0 ; } "
7678-
"void ( * get2 ( ) ) ( ) { return 0 ; } "
7679-
"void ( * get3 ( ) ) ( ) ; "
7680-
"void ( * get4 ( ) ) ( ) ; "
7681-
"} ;";
7682-
ASSERT_EQUALS(expected, tok(code, false));
7667+
{
7668+
const char code[] = "typedef void (*testfp)();\n"
7669+
"struct Fred\n"
7670+
"{\n"
7671+
" testfp get1() { return 0; }\n"
7672+
" void ( * get2 ( ) ) ( ) { return 0 ; }\n"
7673+
" testfp get3();\n"
7674+
" void ( * get4 ( ) ) ( );\n"
7675+
"};";
7676+
const char expected[] = "struct Fred "
7677+
"{ "
7678+
"void ( * get1 ( ) ) ( ) { return 0 ; } "
7679+
"void ( * get2 ( ) ) ( ) { return 0 ; } "
7680+
"void ( * get3 ( ) ) ( ) ; "
7681+
"void ( * get4 ( ) ) ( ) ; "
7682+
"} ;";
7683+
ASSERT_EQUALS(expected, tok(code, false));
7684+
}
7685+
{
7686+
const char code[] = "class Fred {\n"
7687+
" std::string s;\n"
7688+
" const std::string & foo();\n"
7689+
"};\n"
7690+
"const std::string & Fred::foo() { return \"\"; }";
7691+
const char expected[] = "class Fred { "
7692+
"std :: string s ; "
7693+
"const std :: string & foo ( ) ; "
7694+
"} ; "
7695+
"const std :: string & Fred :: foo ( ) { return \"\" ; }";
7696+
ASSERT_EQUALS(expected, tok(code, false));
7697+
}
76837698
}
76847699

76857700
void removeVoidFromFunction() {

0 commit comments

Comments
 (0)