STL: suspicious condition when using std::find
This commit is contained in:
parent
980a810995
commit
4407aabe55
|
@ -557,6 +557,20 @@ void CheckStl::if_find()
|
|||
if_findError(tok, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (Token::Match(tok, "if ( !| std :: find|find_if ("))
|
||||
{
|
||||
// goto '(' for the find
|
||||
tok = tok->tokAt(4);
|
||||
if (tok->isName())
|
||||
tok = tok->next();
|
||||
|
||||
// check that result is checked properly
|
||||
if (Token::simpleMatch(tok->link(), ") )"))
|
||||
{
|
||||
if_findError(tok, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -682,6 +682,25 @@ private:
|
|||
" if (s.find(123) != s.end()) { }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
|
||||
// ---------------------------
|
||||
// std::find
|
||||
// ---------------------------
|
||||
|
||||
// error
|
||||
check("void f()\n"
|
||||
"{\n"
|
||||
" if (std::find(a,b,c)) { }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
|
||||
|
||||
// ok
|
||||
check("void f()\n"
|
||||
"{\n"
|
||||
" if (std::find(a,b,c) != c) { }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue