Fixed #4447 (false positive: (style) Variable 'X' is assigned a value that is never used (goto))

This commit is contained in:
Alexander Mai 2013-03-28 06:44:37 +01:00 committed by Daniel Marjamäki
parent 3f57b5a6ca
commit 7902cd0123
2 changed files with 37 additions and 0 deletions

View File

@ -763,6 +763,11 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.clear(); variables.clear();
break; break;
} }
if (Token::Match(tok, "goto")) { // https://sourceforge.net/apps/trac/cppcheck/ticket/4447
variables.clear();
break;
}
// bailout when for_each is used // bailout when for_each is used
if (Token::Match(tok,"%var% (") && Token::simpleMatch(tok->linkAt(1),") {")) { if (Token::Match(tok,"%var% (") && Token::simpleMatch(tok->linkAt(1),") {")) {

View File

@ -150,6 +150,7 @@ private:
TEST_CASE(localvarAnd); // #3672 TEST_CASE(localvarAnd); // #3672
TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch
TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe
TEST_CASE(localvarUnusedGoto); // #4447, #4558 goto
TEST_CASE(crash1); TEST_CASE(crash1);
TEST_CASE(usingNamespace); // #4585 TEST_CASE(usingNamespace); // #4585
@ -3614,6 +3615,37 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvarUnusedGoto() {
// #4447
functionVariableUsage("bool f(const int &i) {\n"
" int X = i;\n"
"label:\n"
" if ( X == 0 ) {\n"
" X -= 101;\n"
" return true;\n"
" }\n"
" if ( X < 1001 ) {\n"
" X += 1;\n"
" goto label;\n"
" }\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #4558
functionVariableUsage("int f() {\n"
" int i,j=0;\n"
" start:\n"
" i=j;\n"
" i++;\n"
" j=i;\n"
" if (i<3)\n"
" goto start;\n"
" return i;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void crash1() { void crash1() {
functionVariableUsage("SAL_WNODEPRECATED_DECLARATIONS_PUSH\n" functionVariableUsage("SAL_WNODEPRECATED_DECLARATIONS_PUSH\n"
"void convertToTokenArray() {\n" "void convertToTokenArray() {\n"