From 4407aabe556db23576dc31a6c7fbbabd2b98cea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 28 Feb 2010 07:04:58 +0100 Subject: [PATCH] STL: suspicious condition when using std::find --- lib/checkstl.cpp | 14 ++++++++++++++ test/teststl.cpp | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 77c0e7e06..0ff39c923 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -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); + } + } } } diff --git a/test/teststl.cpp b/test/teststl.cpp index 1d0f835f0..7a4af7da3 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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()); }