Skip to content

Commit

Permalink
STL: suspicious condition when using std::find
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Marjamäki committed Feb 28, 2010
1 parent 980a810 commit 4407aab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/checkstl.cpp
Expand Up @@ -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);
}
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/teststl.cpp
Expand Up @@ -682,6 +682,25 @@ class TestStl : public TestFixture
" 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());
}


Expand Down

0 comments on commit 4407aab

Please sign in to comment.