Skip to content

Commit a156364

Browse files
Partial fix for #14911 FN knownConditionTrueFalse for number literals (regression) (#8737)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 71de675 commit a156364

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

lib/checkcondition.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,10 +1575,19 @@ void CheckConditionImpl::alwaysTrueFalse()
15751575
continue;
15761576
if (Token::simpleMatch(tok->astParent(), "return") && Token::Match(tok, ".|%var%"))
15771577
continue;
1578-
if (Token::Match(tok, "%num%|%bool%|%char%"))
1579-
continue;
1580-
if (Token::Match(tok, "! %num%|%bool%|%char%"))
1581-
continue;
1578+
bool warnForNumber = false;
1579+
if (Token::Match(tok, "%num%|%bool%|%char%")) {
1580+
const bool isZeroOrOne = (tok->getKnownIntValue() >> 1) == 0;
1581+
warnForNumber = !isZeroOrOne && tok->tokType() == Token::eNumber && tok->astParent() == condition->astParent();
1582+
if (!warnForNumber)
1583+
continue;
1584+
}
1585+
if (Token::Match(tok, "! %num%|%bool%|%char%")) {
1586+
const bool isZeroOrOne = tok->next()->hasKnownIntValue() && (tok->next()->getKnownIntValue() >> 1) == 0;
1587+
warnForNumber = !isZeroOrOne && tok->next()->tokType() == Token::eNumber && tok->astParent() == condition->astParent();
1588+
if (!warnForNumber)
1589+
continue;
1590+
}
15821591
if (Token::Match(tok, "%oror%|&&")) {
15831592
bool bail = false;
15841593
for (const Token* op : { tok->astOperand1(), tok->astOperand2() }) {
@@ -1603,7 +1612,7 @@ void CheckConditionImpl::alwaysTrueFalse()
16031612
true))
16041613
continue;
16051614

1606-
if (!pedantic && isConstVarExpression(tok, [](const Token* tok) {
1615+
if (!pedantic && !warnForNumber && isConstVarExpression(tok, [](const Token* tok) {
16071616
return Token::Match(tok, "[|(|&|+|-|*|/|%|^|>>|<<") && !Token::simpleMatch(tok, "( )");
16081617
}))
16091618
continue;

test/testcondition.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4949,6 +4949,28 @@ class TestCondition : public TestFixture {
49494949
" return x ? false : true;\n"
49504950
"}\n");
49514951
ASSERT_EQUALS("", errout_str());
4952+
4953+
check("void f() {\n"
4954+
" if (42) {}\n"
4955+
" if (42U) {}\n"
4956+
" if (42L) {}\n"
4957+
" if (42UL) {}\n"
4958+
" if (42LL) {}\n"
4959+
" if (042) {}\n"
4960+
" if (0x42) {}\n"
4961+
" if (0b101010) {}\n"
4962+
" if (!42) {}\n"
4963+
"}\n");
4964+
ASSERT_EQUALS("[test.cpp:2:9]: (style) Condition '42' is always true [knownConditionTrueFalse]\n"
4965+
"[test.cpp:3:9]: (style) Condition '42U' is always true [knownConditionTrueFalse]\n"
4966+
"[test.cpp:4:9]: (style) Condition '42L' is always true [knownConditionTrueFalse]\n"
4967+
"[test.cpp:5:9]: (style) Condition '42UL' is always true [knownConditionTrueFalse]\n"
4968+
"[test.cpp:6:9]: (style) Condition '42LL' is always true [knownConditionTrueFalse]\n"
4969+
"[test.cpp:7:9]: (style) Condition '042' is always true [knownConditionTrueFalse]\n"
4970+
"[test.cpp:8:9]: (style) Condition '0x42' is always true [knownConditionTrueFalse]\n"
4971+
"[test.cpp:9:9]: (style) Condition '0b101010' is always true [knownConditionTrueFalse]\n"
4972+
"[test.cpp:10:9]: (style) Condition '!42' is always false [knownConditionTrueFalse]\n",
4973+
errout_str());
49524974
}
49534975

49544976
void alwaysTrueSymbolic()

0 commit comments

Comments
 (0)