From 9ab4f9976d7b8c7e80f154b9d74105753ff01652 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 29 Aug 2022 12:25:10 +0200 Subject: [PATCH] Fix #10426 FN (style) Condition 's.empty()' is always false (#4414) * Fix #10426 FN (style) Condition 's.empty()' is always false * Fix test --- lib/checkcondition.cpp | 2 +- test/cfg/qt.cpp | 2 +- test/testcondition.cpp | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 97e378cfe..eaf0ac338 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1516,7 +1516,7 @@ void CheckCondition::alwaysTrueFalse() tok->astParent() && Token::Match(tok->astTop()->astOperand1(), "if|while") && !tok->astTop()->astOperand1()->isConstexpr() && (Token::Match(tok->astParent(), "%oror%|&&") || Token::Match(tok->astParent()->astOperand1(), "if|while")); const bool constValExpr = tok->isNumber() && Token::Match(tok->astParent(),"%oror%|&&|?"); // just one number in boolean expression - const bool compExpr = Token::Match(tok, "%comp%|!"); // a compare expression + const bool compExpr = Token::Match(tok, "%comp%|!|("); // a compare expression const bool ternaryExpression = Token::simpleMatch(tok->astParent(), "?"); const bool returnExpression = Token::simpleMatch(tok->astTop(), "return") && (tok->isComparisonOp() || Token::Match(tok, "&&|%oror%")); diff --git a/test/cfg/qt.cpp b/test/cfg/qt.cpp index 736fbf2ee..c793e48fc 100644 --- a/test/cfg/qt.cpp +++ b/test/cfg/qt.cpp @@ -32,7 +32,7 @@ void QString1(QString s) int QString2() { QString s; - // FIXME cppcheck-suppress reademptycontainer + // cppcheck-suppress knownConditionTrueFalse return s.size(); } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index df87884af..f85fdebab 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4305,6 +4305,18 @@ private: " else if (x < 1) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #10426 + check("void f() {\n" + " std::string s;\n" + " for (; !s.empty();) {}\n" + " for (; s.empty();) {}\n" + " if (s.empty()) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Condition '!s.empty()' is always false\n" + "[test.cpp:4]: (style) Condition 's.empty()' is always true\n" + "[test.cpp:5]: (style) Condition 's.empty()' is always true\n", + errout.str()); } void alwaysTrueSymbolic()