Fix #10426 FN (style) Condition 's.empty()' is always false (#4414)

* Fix #10426 FN (style) Condition 's.empty()' is always false

* Fix test
This commit is contained in:
chrchr-github 2022-08-29 12:25:10 +02:00 committed by GitHub
parent 1e14e360cb
commit 9ab4f9976d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -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%"));

View File

@ -32,7 +32,7 @@ void QString1(QString s)
int QString2()
{
QString s;
// FIXME cppcheck-suppress reademptycontainer
// cppcheck-suppress knownConditionTrueFalse
return s.size();
}

View File

@ -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()