Skip to content

Commit 9ab4f99

Browse files
Fix #10426 FN (style) Condition 's.empty()' is always false (#4414)
* Fix #10426 FN (style) Condition 's.empty()' is always false * Fix test
1 parent 1e14e36 commit 9ab4f99

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

lib/checkcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ void CheckCondition::alwaysTrueFalse()
15161516
tok->astParent() && Token::Match(tok->astTop()->astOperand1(), "if|while") && !tok->astTop()->astOperand1()->isConstexpr() &&
15171517
(Token::Match(tok->astParent(), "%oror%|&&") || Token::Match(tok->astParent()->astOperand1(), "if|while"));
15181518
const bool constValExpr = tok->isNumber() && Token::Match(tok->astParent(),"%oror%|&&|?"); // just one number in boolean expression
1519-
const bool compExpr = Token::Match(tok, "%comp%|!"); // a compare expression
1519+
const bool compExpr = Token::Match(tok, "%comp%|!|("); // a compare expression
15201520
const bool ternaryExpression = Token::simpleMatch(tok->astParent(), "?");
15211521
const bool returnExpression = Token::simpleMatch(tok->astTop(), "return") && (tok->isComparisonOp() || Token::Match(tok, "&&|%oror%"));
15221522

test/cfg/qt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void QString1(QString s)
3232
int QString2()
3333
{
3434
QString s;
35-
// FIXME cppcheck-suppress reademptycontainer
35+
// cppcheck-suppress knownConditionTrueFalse
3636
return s.size();
3737
}
3838

test/testcondition.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4305,6 +4305,18 @@ class TestCondition : public TestFixture {
43054305
" else if (x < 1) {}\n"
43064306
"}\n");
43074307
ASSERT_EQUALS("", errout.str());
4308+
4309+
// #10426
4310+
check("void f() {\n"
4311+
" std::string s;\n"
4312+
" for (; !s.empty();) {}\n"
4313+
" for (; s.empty();) {}\n"
4314+
" if (s.empty()) {}\n"
4315+
"}\n");
4316+
ASSERT_EQUALS("[test.cpp:3]: (style) Condition '!s.empty()' is always false\n"
4317+
"[test.cpp:4]: (style) Condition 's.empty()' is always true\n"
4318+
"[test.cpp:5]: (style) Condition 's.empty()' is always true\n",
4319+
errout.str());
43084320
}
43094321

43104322
void alwaysTrueSymbolic()

0 commit comments

Comments
 (0)