Skip to content

Commit 3bdcf68

Browse files
committed
Fixed false positive in CheckUnusedVar::checkFunctionVariableUsage(): Bailout when break; is encountered
See also: https://sourceforge.net/p/cppcheck/discussion/general/thread/1c169dc5/
1 parent d7bf0f4 commit 3bdcf68

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
769769
variables.clear();
770770
break;
771771
}
772-
if (Token::simpleMatch(tok, "goto")) { // https://sourceforge.net/apps/trac/cppcheck/ticket/4447
772+
if (Token::Match(tok, "goto|break")) { // #4447
773773
variables.clear();
774774
break;
775775
}

test/testunusedvar.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,9 +3770,25 @@ class TestUnusedVar : public TestFixture {
37703770
" }\n"
37713771
" return ptr;\n"
37723772
"}");
3773-
3774-
// Don't write an error that "a" is not used
3775-
ASSERT_EQUALS("", errout.str());
3773+
ASSERT_EQUALS("", errout.str()); // Don't write an error that "a" is not used
3774+
3775+
functionVariableUsage("void x() {\n"
3776+
" unsigned char* pcOctet = NULL;\n"
3777+
" float fValeur;\n"
3778+
" switch (pnodeCurrent->left.pnode->usLen) {\n"
3779+
" case 4:\n"
3780+
" fValeur = (float)pevalDataLeft->data.fd;\n"
3781+
" pcOctet = (unsigned char*)&fValeur;\n"
3782+
" break;\n"
3783+
" case 8:\n"
3784+
" pcOctet = (unsigned char*)&pevalDataLeft->data.fd;\n"
3785+
" break;\n"
3786+
" }\n"
3787+
" for (iIndice = 1; iIndice <= (pnodeCurrent->usLen / 2); iIndice++) {\n"
3788+
" *pcData = gacHexChar[(*pcOctet >> 4) & 0x0F];\n"
3789+
" }\n"
3790+
"}");
3791+
ASSERT_EQUALS("", errout.str()); // Don't write an error that "fValeur" is not used
37763792
}
37773793

37783794
void localvarNULL() { // #4203 - Setting NULL value is not redundant - it is safe

0 commit comments

Comments
 (0)