Message refactorization: checkstl.cpp
This commit is contained in:
parent
fc53b15fa4
commit
2aae8381cc
133
lib/checkstl.cpp
133
lib/checkstl.cpp
|
@ -35,13 +35,26 @@ void CheckStl::invalidIteratorError(const Token *tok, const std::string &iterato
|
||||||
|
|
||||||
void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2)
|
void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "iterators", "Same iterator is used with both " + container1 + " and " + container2);
|
reportError(tok, Severity::error, "iterators", "Same iterator is used with different containers '" + container1 + "' and '" + container2 + "'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error message used when dereferencing an iterator that has been erased..
|
// Error message used when dereferencing an iterator that has been erased..
|
||||||
void CheckStl::dereferenceErasedError(const Token *tok, const std::string &itername)
|
void CheckStl::dereferenceErasedError(const Token *erase, const Token* deref, const std::string &itername)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "eraseDereference", "Dereferenced iterator '" + itername + "' has been erased");
|
if (erase) {
|
||||||
|
std::list<const Token*> callstack;
|
||||||
|
callstack.push_back(deref);
|
||||||
|
callstack.push_back(erase);
|
||||||
|
reportError(callstack, Severity::error, "eraseDereference",
|
||||||
|
"Iterator '" + itername + "' used after element has been erased.\n"
|
||||||
|
"The iterator '" + itername + "' is invalid after the element it pointed to has been erased. "
|
||||||
|
"Dereferencing or comparing it with another iterator is invalid operation.");
|
||||||
|
} else {
|
||||||
|
reportError(deref, Severity::error, "eraseDereference",
|
||||||
|
"Invalid iterator '" + itername + "' used.\n"
|
||||||
|
"The iterator '" + itername + "' is invalid before being assigned. "
|
||||||
|
"Dereferencing or comparing it with another iterator is invalid operation.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::iterators()
|
void CheckStl::iterators()
|
||||||
|
@ -68,6 +81,8 @@ void CheckStl::iterators()
|
||||||
// When "validatingToken" is reached the validIterator is set to true
|
// When "validatingToken" is reached the validIterator is set to true
|
||||||
const Token* validatingToken = 0;
|
const Token* validatingToken = 0;
|
||||||
|
|
||||||
|
const Token* eraseToken = 0;
|
||||||
|
|
||||||
// Scan through the rest of the code and see if the iterator is
|
// Scan through the rest of the code and see if the iterator is
|
||||||
// used against other containers.
|
// used against other containers.
|
||||||
for (const Token *tok2 = var->nameToken(); tok2 != var->scope()->classEnd; tok2 = tok2->next()) {
|
for (const Token *tok2 = var->nameToken(); tok2 != var->scope()->classEnd; tok2 = tok2->next()) {
|
||||||
|
@ -119,6 +134,7 @@ void CheckStl::iterators()
|
||||||
// invalidate the iterator if it is erased
|
// invalidate the iterator if it is erased
|
||||||
else if (tok2->strAt(2) == std::string("erase")) {
|
else if (tok2->strAt(2) == std::string("erase")) {
|
||||||
validIterator = false;
|
validIterator = false;
|
||||||
|
eraseToken = tok2;
|
||||||
invalidationScope = tok2->scope();
|
invalidationScope = tok2->scope();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,10 +177,10 @@ void CheckStl::iterators()
|
||||||
|
|
||||||
// Dereferencing invalid iterator?
|
// Dereferencing invalid iterator?
|
||||||
else if (!validIterator && Token::Match(tok2, "* %varid%", iteratorId)) {
|
else if (!validIterator && Token::Match(tok2, "* %varid%", iteratorId)) {
|
||||||
dereferenceErasedError(tok2, tok2->strAt(1));
|
dereferenceErasedError(eraseToken, tok2, tok2->strAt(1));
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
} else if (!validIterator && Token::Match(tok2, "%varid% . %var%", iteratorId)) {
|
} else if (!validIterator && Token::Match(tok2, "%varid% . %var%", iteratorId)) {
|
||||||
dereferenceErasedError(tok2, tok2->str());
|
dereferenceErasedError(eraseToken, tok2, tok2->str());
|
||||||
tok2 = tok2->tokAt(2);
|
tok2 = tok2->tokAt(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,7 +203,7 @@ void CheckStl::iterators()
|
||||||
// Error message for bad iterator usage..
|
// Error message for bad iterator usage..
|
||||||
void CheckStl::mismatchingContainersError(const Token *tok)
|
void CheckStl::mismatchingContainersError(const Token *tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "mismatchingContainers", "Iterators of mismatching containers are used together.");
|
reportError(tok, Severity::error, "mismatchingContainers", "Iterators of different containers are used together.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::mismatchingContainers()
|
void CheckStl::mismatchingContainers()
|
||||||
|
@ -302,9 +318,9 @@ void CheckStl::stlOutOfBounds()
|
||||||
void CheckStl::stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var, bool at)
|
void CheckStl::stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var, bool at)
|
||||||
{
|
{
|
||||||
if (at)
|
if (at)
|
||||||
reportError(tok, Severity::error, "stlOutOfBounds", "When " + num + "==" + var + ".size(), " + var + ".at(" + num + ") is out of bounds");
|
reportError(tok, Severity::error, "stlOutOfBounds", "When " + num + "==" + var + ".size(), " + var + ".at(" + num + ") is out of bounds.");
|
||||||
else
|
else
|
||||||
reportError(tok, Severity::error, "stlOutOfBounds", "When " + num + "==" + var + ".size(), " + var + "[" + num + "] is out of bounds");
|
reportError(tok, Severity::error, "stlOutOfBounds", "When " + num + "==" + var + ".size(), " + var + "[" + num + "] is out of bounds.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -331,7 +347,7 @@ public:
|
||||||
if (! Token::simpleMatch(tok, ") {"))
|
if (! Token::simpleMatch(tok, ") {"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
EraseCheckLoop c(checkStl, it->varId());
|
EraseCheckLoop c(checkStl, it->varId(), it);
|
||||||
std::list<ExecutionPath *> checks;
|
std::list<ExecutionPath *> checks;
|
||||||
checks.push_back(c.copy());
|
checks.push_back(c.copy());
|
||||||
ExecutionPath::checkScope(tok->tokAt(2), checks);
|
ExecutionPath::checkScope(tok->tokAt(2), checks);
|
||||||
|
@ -346,13 +362,16 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** Startup constructor */
|
/** Startup constructor */
|
||||||
EraseCheckLoop(Check *o, unsigned int varid)
|
EraseCheckLoop(Check *o, unsigned int varid, const Token* usetoken)
|
||||||
: ExecutionPath(o, varid), eraseToken(0) {
|
: ExecutionPath(o, varid), eraseToken(0), useToken(usetoken) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief token where iterator is erased (non-zero => the iterator is invalid) */
|
/** @brief token where iterator is erased (non-zero => the iterator is invalid) */
|
||||||
const Token *eraseToken;
|
const Token *eraseToken;
|
||||||
|
|
||||||
|
/** @brief name of the iterator */
|
||||||
|
const Token* useToken;
|
||||||
|
|
||||||
/** @brief Copy this check. Called from the ExecutionPath baseclass. */
|
/** @brief Copy this check. Called from the ExecutionPath baseclass. */
|
||||||
ExecutionPath *copy() {
|
ExecutionPath *copy() {
|
||||||
return new EraseCheckLoop(*this);
|
return new EraseCheckLoop(*this);
|
||||||
|
@ -436,7 +455,7 @@ private:
|
||||||
if (c && c->eraseToken) {
|
if (c && c->eraseToken) {
|
||||||
CheckStl *checkStl = dynamic_cast<CheckStl *>(c->owner);
|
CheckStl *checkStl = dynamic_cast<CheckStl *>(c->owner);
|
||||||
if (checkStl) {
|
if (checkStl) {
|
||||||
checkStl->eraseError(c->eraseToken);
|
checkStl->dereferenceErasedError(c->eraseToken, c->useToken, c->useToken->str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -488,14 +507,6 @@ void CheckStl::erase()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error message for bad iterator usage..
|
|
||||||
void CheckStl::eraseError(const Token *tok)
|
|
||||||
{
|
|
||||||
reportError(tok, Severity::error, "erase",
|
|
||||||
"Dangerous iterator usage after erase()-method.\n"
|
|
||||||
"The iterator is invalid after it has been used in erase() function. "
|
|
||||||
"Dereferencing or comparing it with another iterator is invalid operation.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CheckStl::pushback()
|
void CheckStl::pushback()
|
||||||
{
|
{
|
||||||
|
@ -514,6 +525,7 @@ void CheckStl::pushback()
|
||||||
// Count { , } and parentheses for tok2
|
// Count { , } and parentheses for tok2
|
||||||
int indent = 0;
|
int indent = 0;
|
||||||
bool invalidPointer = false;
|
bool invalidPointer = false;
|
||||||
|
std::string function;
|
||||||
for (const Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next()) {
|
for (const Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next()) {
|
||||||
if (tok2->str() == "{" || tok2->str() == "(")
|
if (tok2->str() == "{" || tok2->str() == "(")
|
||||||
++indent;
|
++indent;
|
||||||
|
@ -525,15 +537,17 @@ void CheckStl::pushback()
|
||||||
}
|
}
|
||||||
|
|
||||||
// push_back on vector..
|
// push_back on vector..
|
||||||
if (Token::Match(tok2, "%varid% . push_front|push_back|resize|reserve", containerId))
|
if (Token::Match(tok2, "%varid% . push_front|push_back|resize|reserve", containerId)) {
|
||||||
invalidPointer = true;
|
invalidPointer = true;
|
||||||
|
function = tok2->strAt(2);
|
||||||
|
}
|
||||||
|
|
||||||
// Using invalid pointer..
|
// Using invalid pointer..
|
||||||
if (invalidPointer && tok2->varId() == pointerId) {
|
if (invalidPointer && tok2->varId() == pointerId) {
|
||||||
if (tok2->previous()->str() == "*")
|
if (tok2->previous()->str() == "*")
|
||||||
invalidPointerError(tok2, tok2->str());
|
invalidPointerError(tok2, function, tok2->str());
|
||||||
else if (tok2->next()->str() == ".")
|
else if (tok2->next()->str() == ".")
|
||||||
invalidPointerError(tok2, tok2->str());
|
invalidPointerError(tok2, function, tok2->str());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -655,14 +669,14 @@ void CheckStl::pushback()
|
||||||
// Error message for bad iterator usage..
|
// Error message for bad iterator usage..
|
||||||
void CheckStl::invalidIteratorError(const Token *tok, const std::string &func, const std::string &iterator_name)
|
void CheckStl::invalidIteratorError(const Token *tok, const std::string &func, const std::string &iterator_name)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "invalidIterator2", "After " + func + ", the iterator '" + iterator_name + "' may be invalid");
|
reportError(tok, Severity::error, "invalidIterator2", "After " + func + "(), the iterator '" + iterator_name + "' may be invalid.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Error message for bad iterator usage..
|
// Error message for bad iterator usage..
|
||||||
void CheckStl::invalidPointerError(const Token *tok, const std::string &pointer_name)
|
void CheckStl::invalidPointerError(const Token *tok, const std::string &func, const std::string &pointer_name)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "invalidPointer", "Invalid pointer '" + pointer_name + "' after push_back / push_front");
|
reportError(tok, Severity::error, "invalidPointer", "Invalid pointer '" + pointer_name + "' after " + func + "().");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -708,11 +722,10 @@ void CheckStl::stlBoundries()
|
||||||
void CheckStl::stlBoundriesError(const Token *tok, const std::string &container_name)
|
void CheckStl::stlBoundriesError(const Token *tok, const std::string &container_name)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "stlBoundries",
|
reportError(tok, Severity::error, "stlBoundries",
|
||||||
"Dangerous container iterator compare using < operator for " + container_name + "\n"
|
"Dangerous iterator comparison using operator< on 'std::" + container_name + "'.\n"
|
||||||
"Container '" + container_name + "' iterator compared with < operator. "
|
"Iterator of container 'std::" + container_name + "' compared with operator<. "
|
||||||
"Using < operator with container type iterators is dangerous since the order of "
|
"This is dangerous since the order of items in the container is not guaranteed. "
|
||||||
"the items is not guaranteed. One should use != operator instead when comparing "
|
"One should use operator!= instead to compare iterators.");
|
||||||
"iterators in the container.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool if_findCompare(const Token * const tokBack)
|
static bool if_findCompare(const Token * const tokBack)
|
||||||
|
@ -854,13 +867,13 @@ void CheckStl::if_findError(const Token *tok, bool str)
|
||||||
{
|
{
|
||||||
if (str)
|
if (str)
|
||||||
reportError(tok, Severity::performance, "stlIfStrFind",
|
reportError(tok, Severity::performance, "stlIfStrFind",
|
||||||
"Inefficient usage of string::find in condition; string::compare would be faster.\n"
|
"Inefficient usage of string::find() in condition; string::compare() would be faster.\n"
|
||||||
"Either inefficent or wrong usage of string::find. string::compare will be faster if "
|
"Either inefficent or wrong usage of string::find(). string::compare() will be faster if "
|
||||||
"string::find's result is compared with 0, because it will not scan the whole "
|
"string::find's result is compared with 0, because it will not scan the whole "
|
||||||
"string. If your intention is to check that there are no findings in the string, "
|
"string. If your intention is to check that there are no findings in the string, "
|
||||||
"you should compare with string::npos.");
|
"you should compare with std::string::npos.");
|
||||||
else
|
else
|
||||||
reportError(tok, Severity::warning, "stlIfFind", "Suspicious condition. The result of find is an iterator, but it is not properly checked.");
|
reportError(tok, Severity::warning, "stlIfFind", "Suspicious condition. The result of find() is an iterator, but it is not properly checked.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -991,9 +1004,9 @@ void CheckStl::redundantCondition()
|
||||||
void CheckStl::redundantIfRemoveError(const Token *tok)
|
void CheckStl::redundantIfRemoveError(const Token *tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "redundantIfRemove",
|
reportError(tok, Severity::style, "redundantIfRemove",
|
||||||
"Redundant checking of STL container element.\n"
|
"Redundant checking of STL container element existence before removing it.\n"
|
||||||
"Redundant checking of STL container element existence before removing it. "
|
"Redundant checking of STL container element existence before removing it. "
|
||||||
"The remove method in the STL will not do anything if element doesn't exist");
|
"It is safe to call the remove method on a non-existing element.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::missingComparison()
|
void CheckStl::missingComparison()
|
||||||
|
@ -1056,15 +1069,23 @@ void CheckStl::missingComparison()
|
||||||
|
|
||||||
void CheckStl::missingComparisonError(const Token *incrementToken1, const Token *incrementToken2)
|
void CheckStl::missingComparisonError(const Token *incrementToken1, const Token *incrementToken2)
|
||||||
{
|
{
|
||||||
|
std::list<const Token*> callstack;
|
||||||
|
callstack.push_back(incrementToken1);
|
||||||
|
callstack.push_back(incrementToken2);
|
||||||
|
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << "Missing bounds check for extra iterator increment in loop.\n"
|
errmsg << "Missing bounds check for extra iterator increment in loop.\n"
|
||||||
<< "The iterator incrementing is suspicious - it is incremented at line "
|
<< "The iterator incrementing is suspicious - it is incremented at line ";
|
||||||
<< incrementToken1->linenr() << " and then at line " << incrementToken2->linenr()
|
if (incrementToken1)
|
||||||
<< " The loop might unintentionally skip an element in the container. "
|
errmsg << incrementToken1->linenr();
|
||||||
|
errmsg << " and then at line ";
|
||||||
|
if (incrementToken2)
|
||||||
|
errmsg << incrementToken2->linenr();
|
||||||
|
errmsg << ". The loop might unintentionally skip an element in the container. "
|
||||||
<< "There is no comparison between these increments to prevent that the iterator is "
|
<< "There is no comparison between these increments to prevent that the iterator is "
|
||||||
<< "incremented beyond the end.";
|
<< "incremented beyond the end.";
|
||||||
|
|
||||||
reportError(incrementToken1, Severity::warning, "StlMissingComparison", errmsg.str());
|
reportError(callstack, Severity::warning, "StlMissingComparison", errmsg.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1196,27 +1217,27 @@ void CheckStl::string_c_str()
|
||||||
|
|
||||||
void CheckStl::string_c_strThrowError(const Token* tok)
|
void CheckStl::string_c_strThrowError(const Token* tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "stlcstrthrow", "Dangerous usage of c_str(). The returned value by c_str() is invalid after throw call.\n"
|
reportError(tok, Severity::error, "stlcstrthrow", "Dangerous usage of c_str(). The value returned by c_str() is invalid after throwing exception.\n"
|
||||||
"Dangerous usage of c_str(). The string is destroyed after the c_str() call so the thrown pointer is invalid.");
|
"Dangerous usage of c_str(). The string is destroyed after the c_str() call so the thrown pointer is invalid.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::string_c_strError(const Token* tok)
|
void CheckStl::string_c_strError(const Token* tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "stlcstr", "Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n"
|
reportError(tok, Severity::error, "stlcstr", "Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n"
|
||||||
"Dangerous usage of c_str(). The c_str() return value is only valid until its string is deleted.");
|
"Dangerous usage of c_str(). The c_str() return value is only valid until its string is deleted.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::string_c_strReturn(const Token* tok)
|
void CheckStl::string_c_strReturn(const Token* tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::performance, "stlcstrReturn", "Returning the result of c_str() in a function that returns std::string is slow and redundant.\n"
|
reportError(tok, Severity::performance, "stlcstrReturn", "Returning the result of c_str() in a function that returns std::string is slow and redundant.\n"
|
||||||
"The conversion from const char* as returned by c_str to std::string creates an unnecessary string copy. Solve that by directly returning the string.");
|
"The conversion from const char* as returned by c_str() to std::string creates an unnecessary string copy. Solve that by directly returning the string.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::string_c_strParam(const Token* tok, unsigned int number)
|
void CheckStl::string_c_strParam(const Token* tok, unsigned int number)
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "Passing the result of c_str() to a function that takes std::string as argument " << number << " is slow and redundant.\n"
|
oss << "Passing the result of c_str() to a function that takes std::string as argument no. " << number << " is slow and redundant.\n"
|
||||||
"The conversion from const char* as returned by c_str to std::string creates an unnecessary string copy. Solve that by directly passing the string.";
|
"The conversion from const char* as returned by c_str() to std::string creates an unnecessary string copy. Solve that by directly passing the string.";
|
||||||
reportError(tok, Severity::performance, "stlcstrParam", oss.str());
|
reportError(tok, Severity::performance, "stlcstrParam", oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1300,15 +1321,15 @@ void CheckStl::checkAutoPointer()
|
||||||
void CheckStl::autoPointerError(const Token *tok)
|
void CheckStl::autoPointerError(const Token *tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "useAutoPointerCopy",
|
reportError(tok, Severity::style, "useAutoPointerCopy",
|
||||||
"Copy 'auto_ptr' pointer to another do not create two equal objects since one has lost its ownership of the pointer.\n"
|
"Copying 'auto_ptr' pointer to another does not create two equal objects since one has lost its ownership of the pointer.\n"
|
||||||
"The auto_ptr has semantics of strict ownership, meaning that the auto_ptr instance is the sole entity responsible for the object's lifetime. If an auto_ptr is copied, the source loses the reference."
|
"'std::auto_ptr' has semantics of strict ownership, meaning that the 'auto_ptr' instance is the sole entity responsible for the object's lifetime. If an 'auto_ptr' is copied, the source looses the reference."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::autoPointerContainerError(const Token *tok)
|
void CheckStl::autoPointerContainerError(const Token *tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "useAutoPointerContainer",
|
reportError(tok, Severity::error, "useAutoPointerContainer",
|
||||||
"You can randomly lose access to pointers if you store 'auto_ptr' pointers in a container because the copy-semantics of 'auto_ptr' are not compatible with containers.\n"
|
"You can randomly lose access to pointers if you store 'auto_ptr' pointers in an STL container.\n"
|
||||||
"An element of container must be able to be copied but 'auto_ptr' does not fulfill this requirement. You should consider to use 'shared_ptr' or 'unique_ptr'. It is suitable for use in containers, because they no longer copy their values, they move them."
|
"An element of container must be able to be copied but 'auto_ptr' does not fulfill this requirement. You should consider to use 'shared_ptr' or 'unique_ptr'. It is suitable for use in containers, because they no longer copy their values, they move them."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1357,10 +1378,10 @@ void CheckStl::uselessCallsReturnValueError(const Token *tok, const std::string
|
||||||
{
|
{
|
||||||
std::ostringstream errmsg;
|
std::ostringstream errmsg;
|
||||||
errmsg << "It is inefficient to call '" << varname << "." << function << "(" << varname << ")' as it always returns 0.\n"
|
errmsg << "It is inefficient to call '" << varname << "." << function << "(" << varname << ")' as it always returns 0.\n"
|
||||||
<< "The 'std::string::" << function << "()' returns zero when given itself as parameter "
|
<< "'std::string::" << function << "()' returns zero when given itself as parameter "
|
||||||
<< "(" << varname << "." << function << "(" << varname << ")). As it is currently the "
|
<< "(" << varname << "." << function << "(" << varname << ")). As it is currently the "
|
||||||
<< "code is inefficient. It is also possible either the string searched ('"
|
<< "code is inefficient. It is possible either the string searched ('"
|
||||||
<< varname << "') or searched for ('" << varname << "') is wrong/mixed in the code?";
|
<< varname << "') or searched for ('" << varname << "') is wrong.";
|
||||||
reportError(tok, Severity::warning, "uselessCallsCompare", errmsg.str());
|
reportError(tok, Severity::warning, "uselessCallsCompare", errmsg.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1370,21 +1391,21 @@ void CheckStl::uselessCallsSwapError(const Token *tok, const std::string &varnam
|
||||||
errmsg << "It is inefficient to swap a object with itself by calling '" << varname << ".swap(" << varname << ")'\n"
|
errmsg << "It is inefficient to swap a object with itself by calling '" << varname << ".swap(" << varname << ")'\n"
|
||||||
<< "The 'swap()' function has no logical effect when given itself as parameter "
|
<< "The 'swap()' function has no logical effect when given itself as parameter "
|
||||||
<< "(" << varname << ".swap(" << varname << ")). As it is currently the "
|
<< "(" << varname << ".swap(" << varname << ")). As it is currently the "
|
||||||
<< "code is inefficient. It is possible either the object or the parameter is wrong/mixed in the code?";
|
<< "code is inefficient. Is the object or the parameter wrong here?";
|
||||||
reportError(tok, Severity::performance, "uselessCallsSwap", errmsg.str());
|
reportError(tok, Severity::performance, "uselessCallsSwap", errmsg.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::uselessCallsSubstrError(const Token *tok, bool empty)
|
void CheckStl::uselessCallsSubstrError(const Token *tok, bool empty)
|
||||||
{
|
{
|
||||||
if (empty)
|
if (empty)
|
||||||
reportError(tok, Severity::performance, "uselessCallsSubstr", "Useless call of function 'substr' because it returns an empty string.");
|
reportError(tok, Severity::performance, "uselessCallsSubstr", "Ineffective call of function 'substr' because it returns an empty string.");
|
||||||
else
|
else
|
||||||
reportError(tok, Severity::performance, "uselessCallsSubstr", "Useless call of function 'substr' because it returns a copy of the object. Use operator= instead.");
|
reportError(tok, Severity::performance, "uselessCallsSubstr", "Ineffective call of function 'substr' because it returns a copy of the object. Use operator= instead.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::uselessCallsEmptyError(const Token *tok)
|
void CheckStl::uselessCallsEmptyError(const Token *tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::warning, "uselessCallsEmpty", "Useless call of function 'empty()'. Did you intend to call 'clear()' instead?");
|
reportError(tok, Severity::warning, "uselessCallsEmpty", "Ineffective call of function 'empty()'. Did you intend to call 'clear()' instead?");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckStl::uselessCallsRemoveError(const Token *tok, const std::string& function)
|
void CheckStl::uselessCallsRemoveError(const Token *tok, const std::string& function)
|
||||||
|
|
|
@ -91,7 +91,6 @@ public:
|
||||||
* it is bad to dereference it after the erase.
|
* it is bad to dereference it after the erase.
|
||||||
*/
|
*/
|
||||||
void erase();
|
void erase();
|
||||||
void eraseError(const Token *tok);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,6 +135,12 @@ public:
|
||||||
void uselessCalls();
|
void uselessCalls();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dereferencing an erased iterator
|
||||||
|
* @param tok token where error occurs
|
||||||
|
* @param itername iterator name
|
||||||
|
*/
|
||||||
|
void dereferenceErasedError(const Token* erase, const Token* deref, const std::string &itername);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -146,13 +151,6 @@ private:
|
||||||
*/
|
*/
|
||||||
void eraseCheckLoop(const Token *it);
|
void eraseCheckLoop(const Token *it);
|
||||||
|
|
||||||
/**
|
|
||||||
* Dereferencing an erased iterator
|
|
||||||
* @param tok token where error occurs
|
|
||||||
* @param itername iterator name
|
|
||||||
*/
|
|
||||||
void dereferenceErasedError(const Token *tok, const std::string &itername);
|
|
||||||
|
|
||||||
void missingComparisonError(const Token *incrementToken1, const Token *incrementToken2);
|
void missingComparisonError(const Token *incrementToken1, const Token *incrementToken2);
|
||||||
void string_c_strThrowError(const Token *tok);
|
void string_c_strThrowError(const Token *tok);
|
||||||
void string_c_strError(const Token *tok);
|
void string_c_strError(const Token *tok);
|
||||||
|
@ -164,7 +162,7 @@ private:
|
||||||
void iteratorsError(const Token *tok, const std::string &container1, const std::string &container2);
|
void iteratorsError(const Token *tok, const std::string &container1, const std::string &container2);
|
||||||
void mismatchingContainersError(const Token *tok);
|
void mismatchingContainersError(const Token *tok);
|
||||||
void invalidIteratorError(const Token *tok, const std::string &func, const std::string &iterator_name);
|
void invalidIteratorError(const Token *tok, const std::string &func, const std::string &iterator_name);
|
||||||
void invalidPointerError(const Token *tok, const std::string &pointer_name);
|
void invalidPointerError(const Token *tok, const std::string &func, const std::string &pointer_name);
|
||||||
void stlBoundriesError(const Token *tok, const std::string &container_name);
|
void stlBoundriesError(const Token *tok, const std::string &container_name);
|
||||||
void if_findError(const Token *tok, bool str);
|
void if_findError(const Token *tok, bool str);
|
||||||
void sizeError(const Token *tok);
|
void sizeError(const Token *tok);
|
||||||
|
@ -185,11 +183,10 @@ private:
|
||||||
c.invalidIteratorError(0, "iterator");
|
c.invalidIteratorError(0, "iterator");
|
||||||
c.iteratorsError(0, "container1", "container2");
|
c.iteratorsError(0, "container1", "container2");
|
||||||
c.mismatchingContainersError(0);
|
c.mismatchingContainersError(0);
|
||||||
c.dereferenceErasedError(0, "iter");
|
c.dereferenceErasedError(0, 0, "iter");
|
||||||
c.stlOutOfBoundsError(0, "i", "foo", false);
|
c.stlOutOfBoundsError(0, "i", "foo", false);
|
||||||
c.eraseError(0);
|
|
||||||
c.invalidIteratorError(0, "push_back|push_front|insert", "iterator");
|
c.invalidIteratorError(0, "push_back|push_front|insert", "iterator");
|
||||||
c.invalidPointerError(0, "pointer");
|
c.invalidPointerError(0, "push_back", "pointer");
|
||||||
c.stlBoundriesError(0, "container");
|
c.stlBoundriesError(0, "container");
|
||||||
c.if_findError(0, false);
|
c.if_findError(0, false);
|
||||||
c.if_findError(0, true);
|
c.if_findError(0, true);
|
||||||
|
@ -197,6 +194,7 @@ private:
|
||||||
c.string_c_strReturn(0);
|
c.string_c_strReturn(0);
|
||||||
c.string_c_strParam(0, 0);
|
c.string_c_strParam(0, 0);
|
||||||
c.sizeError(0);
|
c.sizeError(0);
|
||||||
|
c.missingComparisonError(0, 0);
|
||||||
c.redundantIfRemoveError(0);
|
c.redundantIfRemoveError(0);
|
||||||
c.autoPointerError(0);
|
c.autoPointerError(0);
|
||||||
c.autoPointerContainerError(0);
|
c.autoPointerContainerError(0);
|
||||||
|
|
162
test/teststl.cpp
162
test/teststl.cpp
|
@ -153,7 +153,7 @@ private:
|
||||||
" for (list<int>::iterator it = l1.begin(); it != l2.end(); ++it)\n"
|
" for (list<int>::iterator it = l1.begin(); it != l2.end(); ++it)\n"
|
||||||
" { }\n"
|
" { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
|
||||||
|
|
||||||
// Same check with reverse iterator
|
// Same check with reverse iterator
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
|
@ -163,7 +163,7 @@ private:
|
||||||
" for (list<int>::const_reverse_iterator it = l1.rbegin(); it != l2.rend(); ++it)\n"
|
" for (list<int>::const_reverse_iterator it = l1.rbegin(); it != l2.rend(); ++it)\n"
|
||||||
" { }\n"
|
" { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator2() {
|
void iterator2() {
|
||||||
|
@ -177,7 +177,7 @@ private:
|
||||||
" ++it;\n"
|
" ++it;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator3() {
|
void iterator3() {
|
||||||
|
@ -188,7 +188,7 @@ private:
|
||||||
" list<int>::iterator it = l1.begin();\n"
|
" list<int>::iterator it = l1.begin();\n"
|
||||||
" l2.insert(it, 0);\n"
|
" l2.insert(it, 0);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator4() {
|
void iterator4() {
|
||||||
|
@ -212,7 +212,7 @@ private:
|
||||||
" std::vector<int> ints2;\n"
|
" std::vector<int> ints2;\n"
|
||||||
" std::vector<int>::iterator it = std::find(ints1.begin(), ints2.end(), 22);\n"
|
" std::vector<int>::iterator it = std::find(ints1.begin(), ints2.end(), 22);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of mismatching containers are used together.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator6() {
|
void iterator6() {
|
||||||
|
@ -233,7 +233,7 @@ private:
|
||||||
" std::set<int>::iterator it2 = ints2.end();\n"
|
" std::set<int>::iterator it2 = ints2.end();\n"
|
||||||
" ints2.insert(it1, it2);\n"
|
" ints2.insert(it1, it2);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Iterators of mismatching containers are used together.\n", "", errout.str());
|
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Iterators of different containers are used together.\n", "", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator7() {
|
void iterator7() {
|
||||||
|
@ -243,7 +243,7 @@ private:
|
||||||
" std::vector<int> ints2;\n"
|
" std::vector<int> ints2;\n"
|
||||||
" std::vector<int>::iterator it = std::inplace_merge(ints1.begin(), std::advance(ints1.rbegin(), 5), ints2.end());\n"
|
" std::vector<int>::iterator it = std::inplace_merge(ints1.begin(), std::advance(ints1.rbegin(), 5), ints2.end());\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of mismatching containers are used together.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -261,7 +261,7 @@ private:
|
||||||
" std::vector<int> ints2;\n"
|
" std::vector<int> ints2;\n"
|
||||||
" std::vector<int>::iterator it = std::find_first_of(ints1.begin(), ints2.end(), ints1.begin(), ints1.end());\n"
|
" std::vector<int>::iterator it = std::find_first_of(ints1.begin(), ints2.end(), ints1.begin(), ints1.end());\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of mismatching containers are used together.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -269,7 +269,7 @@ private:
|
||||||
" std::vector<int> ints2;\n"
|
" std::vector<int> ints2;\n"
|
||||||
" std::vector<int>::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints1.end());\n"
|
" std::vector<int>::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints1.end());\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of mismatching containers are used together.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -277,7 +277,7 @@ private:
|
||||||
" std::vector<int> ints2;\n"
|
" std::vector<int> ints2;\n"
|
||||||
" std::vector<int>::iterator it = std::find_first_of(foo.bar.begin(), foo.bar.end()-6, ints2.begin(), ints1.end());\n"
|
" std::vector<int>::iterator it = std::find_first_of(foo.bar.begin(), foo.bar.end()-6, ints2.begin(), ints1.end());\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of mismatching containers are used together.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -338,7 +338,7 @@ private:
|
||||||
" ++aI;\n"
|
" ++aI;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
TODO_ASSERT_EQUALS("[test.cpp:14] (error) After insert, the iterator 'aI' may be invalid", "", errout.str());
|
TODO_ASSERT_EQUALS("[test.cpp:14] (error) After insert(), the iterator 'aI' may be invalid.", "", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator10() {
|
void iterator10() {
|
||||||
|
@ -353,7 +353,7 @@ private:
|
||||||
" if (it != s2.end()) continue;\n"
|
" if (it != s2.end()) continue;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) Same iterator is used with both s1 and s2\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:8]: (error) Same iterator is used with different containers 's1' and 's2'.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterator11() {
|
void iterator11() {
|
||||||
|
@ -375,7 +375,7 @@ private:
|
||||||
" std::map<int, int>::const_iterator it = map1.find(123);\n"
|
" std::map<int, int>::const_iterator it = map1.find(123);\n"
|
||||||
" if (it == map2.end()) { }"
|
" if (it == map2.end()) { }"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with both map1 and map2\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'map1' and 'map2'.\n", errout.str());
|
||||||
|
|
||||||
check("void f(std::string &s) {\n"
|
check("void f(std::string &s) {\n"
|
||||||
" int pos = s.find(x);\n"
|
" int pos = s.find(x);\n"
|
||||||
|
@ -397,7 +397,7 @@ private:
|
||||||
" while (it!=a.end())\n"
|
" while (it!=a.end())\n"
|
||||||
" ++it;\n"
|
" ++it;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:9]: (error) Same iterator is used with both t and a\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:9]: (error) Same iterator is used with different containers 't' and 'a'.\n", errout.str());
|
||||||
|
|
||||||
// #4062
|
// #4062
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
|
@ -439,7 +439,7 @@ private:
|
||||||
" ints.erase(iter);\n"
|
" ints.erase(iter);\n"
|
||||||
" std::cout << (*iter) << std::endl;\n"
|
" std::cout << (*iter) << std::endl;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:7]: (error) Dereferenced iterator 'iter' has been erased\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void dereference_break() { // #3644
|
void dereference_break() { // #3644
|
||||||
|
@ -468,7 +468,7 @@ private:
|
||||||
" ints.erase(iter);\n"
|
" ints.erase(iter);\n"
|
||||||
" std::cout << iter->first << std::endl;\n"
|
" std::cout << iter->first << std::endl;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:7]: (error) Dereferenced iterator 'iter' has been erased\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
// Reverse iterator
|
// Reverse iterator
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
|
@ -479,7 +479,7 @@ private:
|
||||||
" ints.erase(iter);\n"
|
" ints.erase(iter);\n"
|
||||||
" std::cout << iter->first << std::endl;\n"
|
" std::cout << iter->first << std::endl;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:7]: (error) Dereferenced iterator 'iter' has been erased\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -492,14 +492,14 @@ private:
|
||||||
" foo[ii] = 0;\n"
|
" foo[ii] = 0;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) When ii==foo.size(), foo[ii] is out of bounds\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str());
|
||||||
|
|
||||||
check("void foo(std::vector<int> foo) {\n"
|
check("void foo(std::vector<int> foo) {\n"
|
||||||
" for (unsigned int ii = 0; ii <= foo.size(); ++ii) {\n"
|
" for (unsigned int ii = 0; ii <= foo.size(); ++ii) {\n"
|
||||||
" foo.at(ii) = 0;\n"
|
" foo.at(ii) = 0;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo.at(ii) is out of bounds\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo.at(ii) is out of bounds.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -580,8 +580,8 @@ private:
|
||||||
" foo.erase(it);\n"
|
" foo.erase(it);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous iterator usage after erase()-method.\n"
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Iterator 'it' used after element has been erased.\n"
|
||||||
"[test.cpp:7]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
"[test.cpp:6] -> [test.cpp:7]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
check("void f(std::list<int> &ints)\n"
|
check("void f(std::list<int> &ints)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -635,7 +635,7 @@ private:
|
||||||
" foo.erase(it);\n"
|
" foo.erase(it);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -645,7 +645,7 @@ private:
|
||||||
" foo.erase(it);\n"
|
" foo.erase(it);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -655,7 +655,7 @@ private:
|
||||||
" foo.erase(it);\n"
|
" foo.erase(it);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -665,7 +665,7 @@ private:
|
||||||
" foo.erase(++it);\n"
|
" foo.erase(++it);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void erase5() {
|
void erase5() {
|
||||||
|
@ -679,7 +679,7 @@ private:
|
||||||
" foo.erase(it);\n"
|
" foo.erase(it);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void erase6() {
|
void erase6() {
|
||||||
|
@ -705,7 +705,7 @@ private:
|
||||||
" break;\n"
|
" break;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -728,7 +728,7 @@ private:
|
||||||
" return;\n"
|
" return;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -905,7 +905,7 @@ private:
|
||||||
" foo.erase(*it);\n"
|
" foo.erase(*it);\n"
|
||||||
" return *it;\n"
|
" return *it;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Dereferenced iterator 'it' has been erased\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -924,7 +924,7 @@ private:
|
||||||
" foo.push_back(123);\n"
|
" foo.push_back(123);\n"
|
||||||
" *it;\n"
|
" *it;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) After push_back, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback2() {
|
void pushback2() {
|
||||||
|
@ -951,7 +951,7 @@ private:
|
||||||
" foo.push_back(123);\n"
|
" foo.push_back(123);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback4() {
|
void pushback4() {
|
||||||
|
@ -963,7 +963,7 @@ private:
|
||||||
" ints.push_back(2);\n"
|
" ints.push_back(2);\n"
|
||||||
" *first;\n"
|
" *first;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:7]: (error) Invalid pointer 'first' after push_back / push_front\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:7]: (error) Invalid pointer 'first' after push_back().\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback5() {
|
void pushback5() {
|
||||||
|
@ -996,7 +996,7 @@ private:
|
||||||
" v.push_back(10);\n"
|
" v.push_back(10);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:9]: (error) After push_back, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:9]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1009,7 +1009,7 @@ private:
|
||||||
" v.push_back(10);\n"
|
" v.push_back(10);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:9]: (error) After push_back, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:9]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback7() {
|
void pushback7() {
|
||||||
|
@ -1023,7 +1023,7 @@ private:
|
||||||
" foo.push_back(123);\n"
|
" foo.push_back(123);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback8() {
|
void pushback8() {
|
||||||
|
@ -1039,7 +1039,7 @@ private:
|
||||||
" sum += *it;\n"
|
" sum += *it;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back, the iterator 'end' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back(), the iterator 'end' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback9() {
|
void pushback9() {
|
||||||
|
@ -1069,7 +1069,7 @@ private:
|
||||||
" foo.reserve(100);\n"
|
" foo.reserve(100);\n"
|
||||||
" *it = 0;\n"
|
" *it = 0;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) After reserve, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) After reserve(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
|
|
||||||
// in loop
|
// in loop
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
|
@ -1082,7 +1082,7 @@ private:
|
||||||
" foo.reserve(123);\n"
|
" foo.reserve(123);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) After reserve, the iterator 'it' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:8]: (error) After reserve(), the iterator 'it' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushback11() {
|
void pushback11() {
|
||||||
|
@ -1106,7 +1106,7 @@ private:
|
||||||
" ints.insert(ints.begin(), 1);\n"
|
" ints.insert(ints.begin(), 1);\n"
|
||||||
" ++iter;\n"
|
" ++iter;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) After insert(), the iterator 'iter' may be invalid.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1123,7 +1123,7 @@ private:
|
||||||
" ints.insert(iter, 1);\n"
|
" ints.insert(iter, 1);\n"
|
||||||
" ints.insert(iter, 2);\n"
|
" ints.insert(iter, 2);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (error) After insert(), the iterator 'iter' may be invalid.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert2() {
|
void insert2() {
|
||||||
|
@ -1180,14 +1180,14 @@ private:
|
||||||
" ;\n"
|
" ;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
|
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous container iterator compare using < operator for " + stlCont[i] + "\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous iterator comparison using operator< on 'std::" + stlCont[i] + "'.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" std::forward_list<int>::iterator it;\n"
|
" std::forward_list<int>::iterator it;\n"
|
||||||
" for (it = ab.begin(); ab.end() > it; ++it) {}\n"
|
" for (it = ab.begin(); ab.end() > it; ++it) {}\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous container iterator compare using < operator for forward_list\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous iterator comparison using operator< on 'std::forward_list'.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void stlBoundries2() {
|
void stlBoundries2() {
|
||||||
|
@ -1219,7 +1219,7 @@ private:
|
||||||
" static set<Foo>::const_iterator current;\n"
|
" static set<Foo>::const_iterator current;\n"
|
||||||
" return 25 > current->bar;\n"
|
" return 25 > current->bar;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dereferenced iterator 'current' has been erased\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid iterator 'current' used.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1234,42 +1234,42 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s.find(12)) { }\n"
|
" if (s.find(12)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// error (pointer)
|
// error (pointer)
|
||||||
check("void f(std::set<int> *s)\n"
|
check("void f(std::set<int> *s)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (*s.find(12)) { }\n"
|
" if (*s.find(12)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// error (array-like pointer)
|
// error (array-like pointer)
|
||||||
check("void f(std::set<int> *s)\n"
|
check("void f(std::set<int> *s)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s[0].find(12)) { }\n"
|
" if (s[0].find(12)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// error (array)
|
// error (array)
|
||||||
check("void f(std::set<int> s [10])\n"
|
check("void f(std::set<int> s [10])\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s[0].find(12)) { }\n"
|
" if (s[0].find(12)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// error (undefined length array)
|
// error (undefined length array)
|
||||||
check("void f(std::set<int> s [])\n"
|
check("void f(std::set<int> s [])\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s[0].find(12)) { }\n"
|
" if (s[0].find(12)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// error (vector)
|
// error (vector)
|
||||||
check("void f(std::vector<std::set<int> > s)\n"
|
check("void f(std::vector<std::set<int> > s)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s[0].find(12)) { }\n"
|
" if (s[0].find(12)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// ok (simple)
|
// ok (simple)
|
||||||
check("void f(std::set<int> s)\n"
|
check("void f(std::set<int> s)\n"
|
||||||
|
@ -1323,7 +1323,7 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (std::find(a,b,c)) { }\n"
|
" if (std::find(a,b,c)) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
|
||||||
|
|
||||||
// ok
|
// ok
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
|
@ -1350,28 +1350,28 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s.find(\"abc\")) { }\n"
|
" if (s.find(\"abc\")) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find in condition; string::compare would be faster.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find() in condition; string::compare() would be faster.\n", errout.str());
|
||||||
|
|
||||||
// error (pointer)
|
// error (pointer)
|
||||||
check("void f(const std::string *s)\n"
|
check("void f(const std::string *s)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (*s.find(\"abc\")) { }\n"
|
" if (*s.find(\"abc\")) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find in condition; string::compare would be faster.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find() in condition; string::compare() would be faster.\n", errout.str());
|
||||||
|
|
||||||
// error (vector)
|
// error (vector)
|
||||||
check("void f(const std::vector<std::string> &s)\n"
|
check("void f(const std::vector<std::string> &s)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (s[0].find(\"abc\")) { }\n"
|
" if (s[0].find(\"abc\")) { }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find in condition; string::compare would be faster.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find() in condition; string::compare() would be faster.\n", errout.str());
|
||||||
|
|
||||||
// #3162
|
// #3162
|
||||||
check("void f(const std::string& s1, const std::string& s2)\n"
|
check("void f(const std::string& s1, const std::string& s2)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if ((!s1.empty()) && (0 == s1.find(s2))) { }\n"
|
" if ((!s1.empty()) && (0 == s1.find(s2))) { }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find in condition; string::compare would be faster.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find() in condition; string::compare() would be faster.\n", errout.str());
|
||||||
|
|
||||||
// #4102
|
// #4102
|
||||||
check("void f(const std::string &define) {\n"
|
check("void f(const std::string &define) {\n"
|
||||||
|
@ -1489,7 +1489,7 @@ private:
|
||||||
" if (haystack.find(needle) != haystack.end())\n"
|
" if (haystack.find(needle) != haystack.end())\n"
|
||||||
" haystack.remove(needle);"
|
" haystack.remove(needle);"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant checking of STL container element.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant checking of STL container element existence before removing it.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void size2() {
|
void size2() {
|
||||||
|
@ -1515,7 +1515,7 @@ private:
|
||||||
" haystack.remove(needle);\n"
|
" haystack.remove(needle);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant checking of STL container element.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant checking of STL container element existence before removing it.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void missingInnerComparison1() {
|
void missingInnerComparison1() {
|
||||||
|
@ -1526,7 +1526,7 @@ private:
|
||||||
" }\n"
|
" }\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (warning) Missing bounds check for extra iterator increment in loop.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Missing bounds check for extra iterator increment in loop.\n", errout.str());
|
||||||
|
|
||||||
check("void f(std::map<int,int> &ints) {\n"
|
check("void f(std::map<int,int> &ints) {\n"
|
||||||
" for (std::map<int,int>::iterator it = ints.begin(); it != ints.end(); ++it) {\n"
|
" for (std::map<int,int>::iterator it = ints.begin(); it != ints.end(); ++it) {\n"
|
||||||
|
@ -1604,51 +1604,51 @@ private:
|
||||||
" std::string errmsg;\n"
|
" std::string errmsg;\n"
|
||||||
" throw errmsg.c_str();\n"
|
" throw errmsg.c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after throw call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after throwing exception.\n", errout.str());
|
||||||
|
|
||||||
check("const char *get_msg() {\n"
|
check("const char *get_msg() {\n"
|
||||||
" std::string errmsg;\n"
|
" std::string errmsg;\n"
|
||||||
" return errmsg.c_str();\n"
|
" return errmsg.c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("const char *get_msg() {\n"
|
check("const char *get_msg() {\n"
|
||||||
" std::ostringstream errmsg;\n"
|
" std::ostringstream errmsg;\n"
|
||||||
" return errmsg.str().c_str();\n"
|
" return errmsg.str().c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("const char *get_msg() {\n"
|
check("const char *get_msg() {\n"
|
||||||
" std::string errmsg;\n"
|
" std::string errmsg;\n"
|
||||||
" return std::string(\"ERROR: \" + errmsg).c_str();\n"
|
" return std::string(\"ERROR: \" + errmsg).c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("const char *get_msg() {\n"
|
check("const char *get_msg() {\n"
|
||||||
" std::string errmsg;\n"
|
" std::string errmsg;\n"
|
||||||
" return (\"ERROR: \" + errmsg).c_str();\n"
|
" return (\"ERROR: \" + errmsg).c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("const char *get_msg() {\n"
|
check("const char *get_msg() {\n"
|
||||||
" std::string errmsg;\n"
|
" std::string errmsg;\n"
|
||||||
" return (\"ERROR: \" + std::string(\"crash me\")).c_str();\n"
|
" return (\"ERROR: \" + std::string(\"crash me\")).c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" std::ostringstream errmsg;\n"
|
" std::ostringstream errmsg;\n"
|
||||||
" const char *c = errmsg.str().c_str();\n"
|
" const char *c = errmsg.str().c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("std::string f();\n"
|
check("std::string f();\n"
|
||||||
"\n"
|
"\n"
|
||||||
"void foo() {\n"
|
"void foo() {\n"
|
||||||
" const char *c = f().c_str();\n"
|
" const char *c = f().c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("class Foo {\n"
|
check("class Foo {\n"
|
||||||
" const char *f();\n"
|
" const char *f();\n"
|
||||||
|
@ -1657,7 +1657,7 @@ private:
|
||||||
" std::string s;\n"
|
" std::string s;\n"
|
||||||
" return s.c_str();\n"
|
" return s.c_str();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("const char* foo() {\n"
|
check("const char* foo() {\n"
|
||||||
" static std::string text;\n"
|
" static std::string text;\n"
|
||||||
|
@ -1702,12 +1702,12 @@ private:
|
||||||
" Foo4(str, str.c_str());\n"
|
" Foo4(str, str.c_str());\n"
|
||||||
" Foo4(str.c_str(), str.c_str());\n"
|
" Foo4(str.c_str(), str.c_str());\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (performance) Passing the result of c_str() to a function that takes std::string as argument 1 is slow and redundant.\n"
|
ASSERT_EQUALS("[test.cpp:8]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
|
||||||
"[test.cpp:10]: (performance) Passing the result of c_str() to a function that takes std::string as argument 2 is slow and redundant.\n"
|
"[test.cpp:10]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 2 is slow and redundant.\n"
|
||||||
"[test.cpp:13]: (performance) Passing the result of c_str() to a function that takes std::string as argument 1 is slow and redundant.\n"
|
"[test.cpp:13]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
|
||||||
"[test.cpp:14]: (performance) Passing the result of c_str() to a function that takes std::string as argument 2 is slow and redundant.\n"
|
"[test.cpp:14]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 2 is slow and redundant.\n"
|
||||||
"[test.cpp:15]: (performance) Passing the result of c_str() to a function that takes std::string as argument 1 is slow and redundant.\n"
|
"[test.cpp:15]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
|
||||||
"[test.cpp:15]: (performance) Passing the result of c_str() to a function that takes std::string as argument 2 is slow and redundant.\n", errout.str());
|
"[test.cpp:15]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 2 is slow and redundant.\n", errout.str());
|
||||||
|
|
||||||
check("void Foo1(const std::string& str) {}\n"
|
check("void Foo1(const std::string& str) {}\n"
|
||||||
"void Foo2(char* c, const std::string str) {}\n"
|
"void Foo2(char* c, const std::string str) {}\n"
|
||||||
|
@ -1765,7 +1765,7 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" return hello().c_str();\n"
|
" return hello().c_str();\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:8]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:8]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
|
|
||||||
check("class Fred {\n"
|
check("class Fred {\n"
|
||||||
" std::string hello();\n"
|
" std::string hello();\n"
|
||||||
|
@ -1779,7 +1779,7 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" return hello().c_str();\n"
|
" return hello().c_str();\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:11]: (error) Dangerous usage of c_str(). The returned value by c_str() is invalid after this call.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:11]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void autoPointer() {
|
void autoPointer() {
|
||||||
|
@ -1822,13 +1822,13 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::vector< std::auto_ptr< ns1::MyClass> > v;\n"
|
" std::vector< std::auto_ptr< ns1::MyClass> > v;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) You can randomly lose access to pointers if you store 'auto_ptr' pointers in a container because the copy-semantics of 'auto_ptr' are not compatible with containers.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) You can randomly lose access to pointers if you store 'auto_ptr' pointers in an STL container.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::vector< auto_ptr< MyClass> > v;\n"
|
" std::vector< auto_ptr< MyClass> > v;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) You can randomly lose access to pointers if you store 'auto_ptr' pointers in a container because the copy-semantics of 'auto_ptr' are not compatible with containers.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) You can randomly lose access to pointers if you store 'auto_ptr' pointers in an STL container.\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1837,7 +1837,7 @@ private:
|
||||||
" auto_ptr<int> y;\n"
|
" auto_ptr<int> y;\n"
|
||||||
" y = x;\n"
|
" y = x;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (style) Copy 'auto_ptr' pointer to another do not create two equal objects since one has lost its ownership of the pointer.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (style) Copying 'auto_ptr' pointer to another does not create two equal objects since one has lost its ownership of the pointer.\n", errout.str());
|
||||||
|
|
||||||
check("std::auto_ptr<A> function();\n"
|
check("std::auto_ptr<A> function();\n"
|
||||||
"int quit;"
|
"int quit;"
|
||||||
|
@ -1961,11 +1961,11 @@ private:
|
||||||
" s1 = s2.substr(x+5-n, 0);\n"
|
" s1 = s2.substr(x+5-n, 0);\n"
|
||||||
" \n"
|
" \n"
|
||||||
"};\n");
|
"};\n");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (performance) Useless call of function \'substr\' because it returns a copy of "
|
ASSERT_EQUALS("[test.cpp:5]: (performance) Ineffective call of function \'substr\' because it returns a copy of "
|
||||||
"the object. Use operator= instead.\n"
|
"the object. Use operator= instead.\n"
|
||||||
"[test.cpp:8]: (performance) Useless call of function \'substr\' because it returns a copy of "
|
"[test.cpp:8]: (performance) Ineffective call of function \'substr\' because it returns a copy of "
|
||||||
"the object. Use operator= instead.\n"
|
"the object. Use operator= instead.\n"
|
||||||
"[test.cpp:9]: (performance) Useless call of function \'substr\' because it returns an empty string.\n", errout.str());
|
"[test.cpp:9]: (performance) Ineffective call of function \'substr\' because it returns an empty string.\n", errout.str());
|
||||||
|
|
||||||
check("int main()\n"
|
check("int main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1978,7 +1978,7 @@ private:
|
||||||
" v.empty();\n"
|
" v.empty();\n"
|
||||||
" return v.empty();\n"
|
" return v.empty();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Useless call of function 'empty()'. Did you intend to call 'clear()' instead?\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective call of function 'empty()'. Did you intend to call 'clear()' instead?\n", errout.str());
|
||||||
|
|
||||||
check("void f() {\n" // #4032
|
check("void f() {\n" // #4032
|
||||||
" const std::string greeting(\"Hello World !!!\");\n"
|
" const std::string greeting(\"Hello World !!!\");\n"
|
||||||
|
|
Loading…
Reference in New Issue