Fixed #3865 (Suspicious condition. The result of find is an iterator, but it is not properly checked.)
This commit is contained in:
parent
9026febaad
commit
2b3e5abef8
|
@ -721,6 +721,22 @@ void CheckStl::stlBoundriesError(const Token *tok, const std::string &container_
|
||||||
"iterators in the container.");
|
"iterators in the container.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool if_findCompare(const Token * const tokBack)
|
||||||
|
{
|
||||||
|
const Token *tok = tokBack;
|
||||||
|
while (tok && tok->str() == ")") {
|
||||||
|
tok = tok->next();
|
||||||
|
if (Token::Match(tok,",|==|!="))
|
||||||
|
return true;
|
||||||
|
if (Token::Match(tok, ") !!{") &&
|
||||||
|
tok->link()->previous() &&
|
||||||
|
(Token::Match(tok->link()->previous(),",|==|!=") ||
|
||||||
|
tok->link()->previous()->isName()))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void CheckStl::if_find()
|
void CheckStl::if_find()
|
||||||
{
|
{
|
||||||
if (!_settings->isEnabled("style"))
|
if (!_settings->isEnabled("style"))
|
||||||
|
@ -749,7 +765,7 @@ void CheckStl::if_find()
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
if (Token::Match(tok, "%var% . find (")) {
|
if (Token::Match(tok, "%var% . find (")) {
|
||||||
if (!Token::Match(tok->linkAt(3), ") &&|)|%oror%"))
|
if (if_findCompare(tok->linkAt(3)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const unsigned int varid = tok->varId();
|
const unsigned int varid = tok->varId();
|
||||||
|
@ -780,7 +796,7 @@ void CheckStl::if_find()
|
||||||
|
|
||||||
if (!Token::simpleMatch(tok2, ". find ("))
|
if (!Token::simpleMatch(tok2, ". find ("))
|
||||||
continue;
|
continue;
|
||||||
if (!Token::Match(tok2->linkAt(2), ") &&|)|%oror%"))
|
if (if_findCompare(tok2->linkAt(2)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const unsigned int varid = tok->varId();
|
const unsigned int varid = tok->varId();
|
||||||
|
@ -827,7 +843,7 @@ void CheckStl::if_find()
|
||||||
|
|
||||||
else if (Token::Match(tok, "std :: find|find_if (")) {
|
else if (Token::Match(tok, "std :: find|find_if (")) {
|
||||||
// check that result is checked properly
|
// check that result is checked properly
|
||||||
if (Token::Match(tok->linkAt(3), ") &&|)|%oror%")) {
|
if (!if_findCompare(tok->linkAt(3))) {
|
||||||
if_findError(tok, false);
|
if_findError(tok, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1268,6 +1268,12 @@ private:
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" if (()) { }\n"
|
" if (()) { }\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
||||||
|
// #3865
|
||||||
|
check("void f() {\n"
|
||||||
|
" if ((std::find(a,b,c)) != b) { }\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void if_str_find() {
|
void if_str_find() {
|
||||||
|
|
Loading…
Reference in New Issue