Permalink
Browse files

Fixed #4102 (False positive: 'find('=') + 1U' can't be replaced with …

…compare)
  • Loading branch information...
1 parent b1a768e commit 0115bb8d24bd72ac1bf1f3ba7a091cbbea3159d3 @danmar committed Oct 7, 2012
Showing with 13 additions and 3 deletions.
  1. +1 −1 .travis.yml
  2. +6 −2 lib/checkstl.cpp
  3. +6 −0 test/teststl.cpp
View
@@ -5,4 +5,4 @@ compiler:
script:
- make test
- $CXX -o cppcheck -O2 cli/*.cpp lib/*.cpp -Ilib
- - ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=duplicateBranch --suppress=stlIfStrFind -q cli gui lib -igui/test
+ - ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=duplicateBranch -q cli gui lib -igui/test
View
@@ -720,14 +720,18 @@ static bool if_findCompare(const Token * const tokBack)
const Token *tok = tokBack;
while (tok && tok->str() == ")") {
tok = tok->next();
- if (Token::Match(tok,",|==|!="))
- return true;
+
if (Token::Match(tok, ") !!{") &&
tok->link()->previous() &&
(Token::Match(tok->link()->previous(),",|==|!=") ||
tok->link()->previous()->isName()))
return true;
}
+
+ if (Token::Match(tok,",|==|!="))
+ return true;
+ if (tok->isArithmeticalOp()) // result is used in some calculation
+ return true; // TODO: check if there is a comparison of the result somewhere
return false;
}
View
@@ -1372,6 +1372,12 @@ class TestStl : public TestFixture {
" if ((!s1.empty()) && (0 == s1.find(s2))) { }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (performance) Inefficient usage of string::find in condition; string::compare would be faster.\n", errout.str());
+
+ // #4102
+ check("void f(const std::string &define) {\n"
+ " if (define.find(\"=\") + 1U == define.size());\n"
+ "}");
+ ASSERT_EQUALS("", errout.str());
}

0 comments on commit 0115bb8

Please sign in to comment.