diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index b2de1b887..263a9b6ee 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -553,6 +553,8 @@ void CheckStl::if_find() // stl container if (Token::Match(decl, "const| std :: %var% < %type% > &|*| %varid%", varid)) if_findError(tok, false); + else if (Token::Match(decl, "const| std :: string &|*| %varid%", varid)) + if_findError(tok, true); } } @@ -576,7 +578,7 @@ void CheckStl::if_find() void CheckStl::if_findError(const Token *tok, bool str) { if (str) - reportError(tok, Severity::possibleStyle, "stlIfStrFind", "Suspicious condition. string::find will return 0 if the string is found at position 0. If this is what you want to check then string::compare is a faster alternative because it doesn't scan through the string."); + reportError(tok, Severity::style, "stlIfStrFind", "Suspicious condition. string::find will return 0 if the string is found at position 0. If this is what you want to check then string::compare is a faster alternative because it doesn't scan through the string."); else reportError(tok, Severity::style, "stlIfFind", "Suspicious condition. The result of find is an iterator, but it is not properly checked."); } diff --git a/test/teststl.cpp b/test/teststl.cpp index a06b03874..268224d23 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -76,6 +76,7 @@ private: // if (str.find("ab")) TEST_CASE(if_find); + TEST_CASE(if_str_find); TEST_CASE(size1); } @@ -736,7 +737,15 @@ private: ASSERT_EQUALS("", errout.str()); } - + void if_str_find() + { + // error + check("void f(const std::string &s)\n" + "{\n" + " if (s.find(\"abc\")) { }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Suspicious condition. string::find will return 0 if the string is found at position 0. If this is what you want to check then string::compare is a faster alternative because it doesn't scan through the string.\n", errout.str()); + } void size1()