Skip to content

Commit ba84196

Browse files
authored
Fix issue 9865: false positive: knownConditionTrueFalse (cppcheck-opensource#2764)
1 parent 0a71869 commit ba84196

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,6 +4325,8 @@ struct ValueFlowConditionHandler {
43254325
if (!values.empty()) {
43264326
bool assign = false;
43274327
visitAstNodes(parent->astOperand2(), [&](Token* tok2) {
4328+
if (tok2 == tok)
4329+
return ChildrenToVisit::done;
43284330
if (isSameExpression(tokenlist->isCPP(), false, cond.vartok, tok2, settings->library, true, false))
43294331
setTokenValue(tok2, values.front(), settings);
43304332
else if (Token::Match(tok2, "++|--|=") && isSameExpression(tokenlist->isCPP(),

test/testcondition.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,6 +3412,18 @@ class TestCondition : public TestFixture {
34123412
"}\n");
34133413
ASSERT_EQUALS("", errout.str());
34143414

3415+
// #9865
3416+
check("void f(const std::string &s) {\n"
3417+
" for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {\n"
3418+
" const unsigned char c = static_cast<unsigned char>(*it);\n"
3419+
" if (c == '0') {}\n"
3420+
" else if ((c == 'a' || c == 'A')\n"
3421+
" || (c == 'b' || c == 'B')) {}\n"
3422+
" else {}\n"
3423+
" }\n"
3424+
"}\n");
3425+
ASSERT_EQUALS("", errout.str());
3426+
34153427
}
34163428

34173429
void alwaysTrueInfer() {

0 commit comments

Comments
 (0)