Added check that detects unreachable code below a 'return' statement
This commit is contained in:
parent
7c32b7b2bb
commit
4d070f04e5
|
@ -784,4 +784,37 @@ void CheckOther::CheckIncompleteStatement()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
// 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" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,9 @@ public:
|
||||||
|
|
||||||
// Incomplete statement. A statement that only contains a constant or variable
|
// Incomplete statement. A statement that only contains a constant or variable
|
||||||
void CheckIncompleteStatement();
|
void CheckIncompleteStatement();
|
||||||
|
|
||||||
|
/** Unreachable code below a 'return' */
|
||||||
|
void unreachableCode();
|
||||||
private:
|
private:
|
||||||
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
|
void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
|
||||||
|
|
||||||
|
|
|
@ -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
|
// Check for various types of incomplete statements that could for example
|
||||||
// mean that an ';' has been added by accident
|
// mean that an ';' has been added by accident
|
||||||
checkOther.CheckIncompleteStatement();
|
checkOther.CheckIncompleteStatement();
|
||||||
|
|
||||||
|
// Unreachable code below a 'return' statement
|
||||||
|
checkOther.unreachableCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue