Skip to content

Commit e5e6f37

Browse files
committed
Fixed danmar#7405 (false positive: (warning) Opposite conditions in nested 'if' blocks lead to a dead code block.)
1 parent 4ca004c commit e5e6f37

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/checkcondition.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,13 @@ void CheckCondition::multiCondition2()
593593
(!tok->varId() && nonlocal)) {
594594
if (Token::Match(tok, "%name% %assign%|++|--"))
595595
break;
596+
if (Token::Match(tok->astParent(), "*|.|[")) {
597+
const Token *parent = tok;
598+
while (Token::Match(parent->astParent(), ".|[") || (Token::simpleMatch(parent->astParent(), "*") && !parent->astParent()->astOperand2()))
599+
parent = parent->astParent();
600+
if (Token::Match(parent->astParent(), "%assign%"))
601+
break;
602+
}
596603
if (Token::Match(tok, "%name% <<|>>") && (!tok->valueType() || !tok->valueType()->isIntegral()))
597604
break;
598605
if (Token::Match(tok, "%name% [")) {

test/testcondition.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,13 @@ class TestCondition : public TestFixture {
15221522
"}");
15231523
ASSERT_EQUALS("", errout.str());
15241524

1525+
check("void test(float *f) {\n" // #7405
1526+
" if(*f>10) {\n"
1527+
" (*f) += 0.1f;\n"
1528+
" if(*f<10) {}\n"
1529+
" }\n"
1530+
"}");
1531+
ASSERT_EQUALS("", errout.str());
15251532
}
15261533

15271534
void oppositeInnerConditionClass() {

0 commit comments

Comments
 (0)