STL: suspicious condition when using std::find

This commit is contained in:
Daniel Marjamäki 2010-02-28 07:04:58 +01:00
parent 980a810995
commit 4407aabe55
2 changed files with 33 additions and 0 deletions

View File

@ -557,6 +557,20 @@ void CheckStl::if_find()
if_findError(tok, false); 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);
}
}
} }
} }

View File

@ -682,6 +682,25 @@ private:
" if (s.find(123) != s.end()) { }\n" " if (s.find(123) != s.end()) { }\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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());
} }