Added check that detects unreachable code below a 'return' statement

This commit is contained in:
Daniel Marjamäki 2008-12-06 16:25:24 +00:00
parent 7c32b7b2bb
commit 4d070f04e5
3 changed files with 41 additions and 2 deletions

View File

@ -783,5 +783,38 @@ void CheckOther::CheckIncompleteStatement()
_errorLogger->reportErr(errmsg.str());
}
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Unreachable code below a 'return'
//---------------------------------------------------------------------------
void CheckOther::unreachableCode()
{
const TOKEN *tok = TOKEN::findmatch( _tokenizer->tokens(), "[;{}] return" );
while ( tok )
{
// Locate the end of the 'return' statement
while ( tok && ! TOKEN::Match(tok, ";") )
tok = tok->next;
// Next token should be either "case", "default" or "}"
if (tok && tok->next && !TOKEN::Match( tok, "; case|default|}"))
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok->next) << ": Unreachable code below a 'return'";
_errorLogger->reportErr(errmsg.str());
}
// Find the next 'return' statement
tok = TOKEN::findmatch( tok, "[;{}] return" );
}
}

View File

@ -69,7 +69,10 @@ public:
void CheckCharVariable();
// Incomplete statement. A statement that only contains a constant or variable
void CheckIncompleteStatement();
void CheckIncompleteStatement();
/** Unreachable code below a 'return' */
void unreachableCode();
private:
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );

View File

@ -319,6 +319,9 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Check for various types of incomplete statements that could for example
// mean that an ';' has been added by accident
checkOther.CheckIncompleteStatement();
// Unreachable code below a 'return' statement
checkOther.unreachableCode();
}
}
//---------------------------------------------------------------------------