Fixed #7588 (Opposite conditions in nested 'if' blocks lead to a dead code block.)

This commit is contained in:
Daniel Marjamäki 2017-02-28 18:46:28 +01:00
parent 2ccd5aec1a
commit 16c06e5714
2 changed files with 16 additions and 2 deletions

View File

@ -471,8 +471,12 @@ void CheckCondition::oppositeInnerCondition()
}
if (Token::Match(tok, "%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables
break;
else if ((tok->varId() && vars.find(tok->varId()) != vars.end()) ||
(!tok->varId() && nonlocal)) {
// bailout if loop is seen.
// TODO: handle loops.
if (Token::Match(tok, "for|while|do"))
break;
if ((tok->varId() && vars.find(tok->varId()) != vars.end()) ||
(!tok->varId() && nonlocal)) {
if (Token::Match(tok, "%name% %assign%|++|--"))
break;
if (Token::Match(tok, "%name% [")) {

View File

@ -1505,6 +1505,16 @@ private:
" }\n"
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
check("void foo(unsigned u) {\n"
" if (u != 0) {\n"
" for (int i=0; i<32; i++) {\n"
" if (u == 0) {}\n" // <- don't warn
" u = x;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// clarify conditions with = and comparison