Skip to content

Commit

Permalink
Fix #12487 FN knownConditionTrueFalse with integer operand (#6082)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 6, 2024
1 parent e93b031 commit c3ab5ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,9 +1540,17 @@ void CheckCondition::alwaysTrueFalse()
continue;
if (Token::Match(tok, "! %num%|%bool%|%char%"))
continue;
if (Token::Match(tok, "%oror%|&&") &&
(tok->astOperand1()->hasKnownIntValue() || tok->astOperand2()->hasKnownIntValue()))
continue;
if (Token::Match(tok, "%oror%|&&")) {
bool bail = false;
for (const Token* op : { tok->astOperand1(), tok->astOperand2() }) {
if (op->hasKnownIntValue() && (!op->isLiteral() || op->isBoolean())) {
bail = true;
break;
}
}
if (bail)
continue;
}
if (Token::simpleMatch(tok, ":"))
continue;
if (Token::Match(tok->astOperand1(), "%name% (") && Token::simpleMatch(tok->astParent(), "return"))
Expand Down
7 changes: 7 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5712,6 +5712,13 @@ class TestCondition : public TestFixture {
" return s.size()>2U && s[0]=='4' && s[0]=='2';\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return value 's[0]=='2'' is always false\n", errout.str());

check("void f(int i) { if (i == 1 || 2) {} }\n"); // #12487
ASSERT_EQUALS("[test.cpp:1]: (style) Condition 'i==1||2' is always true\n", errout.str());

check("enum E { E1 = 1, E2 = 2 };\n"
"void f(int i) { if (i == E1 || E2) {} }\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Condition 'i==E1||E2' is always true\n", errout.str());
}

void pointerAdditionResultNotNull() {
Expand Down

0 comments on commit c3ab5ef

Please sign in to comment.