unreachableCode: Moved warning of a break statement following a return in --style; Added a test case; Minor optimization.
This commit is contained in:
parent
e930525aa4
commit
f6d121443e
|
@ -802,29 +802,27 @@ void CheckOther::CheckIncompleteStatement()
|
||||||
|
|
||||||
void CheckOther::unreachableCode()
|
void CheckOther::unreachableCode()
|
||||||
{
|
{
|
||||||
const Token *tok = Token::findmatch(_tokenizer->tokens(), "[;{}] return");
|
const Token *tok = _tokenizer->tokens();
|
||||||
while (tok)
|
while ((tok = Token::findmatch(tok, "[;{}] return")))
|
||||||
{
|
{
|
||||||
// Goto the 'return' token
|
// Goto the 'return' token
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
// Locate the end of the 'return' statement
|
// Locate the end of the 'return' statement
|
||||||
while (tok && ! Token::Match(tok, ";"))
|
while (tok && tok->str() != ";")
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
while (tok && Token::Match(tok->next(), ";"))
|
while (tok && tok->next() && tok->next()->str() == ";")
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
if (!tok)
|
if (!tok)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// If there is a statement below the return it is unreachable
|
// If there is a statement below the return it is unreachable
|
||||||
if (!Token::Match(tok, "; case|default|}|#") && !Token::Match(tok, "; %var% :"))
|
if ( !Token::Match(tok, "; case|default|}|#") && !Token::Match(tok, "; %var% :")
|
||||||
|
&& ( _settings._checkCodingStyle || !Token::simpleMatch(tok, "; break") ) )
|
||||||
{
|
{
|
||||||
_errorLogger->reportErr(ErrorMessage::unreachableCode(_tokenizer, tok->next()));
|
_errorLogger->reportErr(ErrorMessage::unreachableCode(_tokenizer, tok->next()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the next 'return' statement
|
|
||||||
tok = Token::findmatch(tok, "[;{}] return");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
|
@ -37,6 +37,8 @@ private:
|
||||||
TEST_CASE(delete1);
|
TEST_CASE(delete1);
|
||||||
TEST_CASE(delete2);
|
TEST_CASE(delete2);
|
||||||
|
|
||||||
|
TEST_CASE(unreachable1);
|
||||||
|
|
||||||
TEST_CASE(sprintf1); // Dangerous usage of sprintf
|
TEST_CASE(sprintf1); // Dangerous usage of sprintf
|
||||||
TEST_CASE(sprintf2);
|
TEST_CASE(sprintf2);
|
||||||
TEST_CASE(sprintf3);
|
TEST_CASE(sprintf3);
|
||||||
|
@ -94,6 +96,20 @@ private:
|
||||||
ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str());
|
ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unreachable1()
|
||||||
|
{
|
||||||
|
check("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" switch (p)\n"
|
||||||
|
" {\n"
|
||||||
|
" default:\n"
|
||||||
|
" return 0;\n"
|
||||||
|
" break;\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS(std::string(""), errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void sprintfUsage(const char code[])
|
void sprintfUsage(const char code[])
|
||||||
|
|
Loading…
Reference in New Issue