Improved CheckCondition::multiCondition2 so deeper bugs are found

This commit is contained in:
Daniel Marjamäki 2017-09-03 10:44:22 +02:00
parent f2ec5f24ce
commit 01e65d3e00
2 changed files with 19 additions and 16 deletions

View File

@ -509,11 +509,20 @@ void CheckCondition::multiCondition2()
type = MULTICONDITIONTYPE::INNER; type = MULTICONDITIONTYPE::INNER;
} }
const Token * const endToken = tok->scope()->classEnd; const Token * const endToken = tok->scope()->classEnd;
const Token *ifToken = nullptr;
for (; tok && tok != endToken; tok = tok->next()) { for (; tok && tok != endToken; tok = tok->next()) {
if (Token::simpleMatch(tok, "if (")) { if (Token::simpleMatch(tok, "if (")) {
ifToken = tok; // Condition..
break; const Token *cond2 = tok->next()->astOperand2();
if (type == MULTICONDITIONTYPE::INNER) {
if (isOppositeCond(false, _tokenizer->isCPP(), cond1, cond2, _settings->library, true))
oppositeInnerConditionError(cond1, cond2);
} else {
if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true))
sameConditionAfterEarlyExitError(cond1, cond2);
}
} }
if (Token::Match(tok, "%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables if (Token::Match(tok, "%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables
break; break;
@ -545,19 +554,6 @@ void CheckCondition::multiCondition2()
break; break;
} }
} }
if (!ifToken)
continue;
// Condition..
const Token *cond2 = ifToken->next()->astOperand2();
if (type == MULTICONDITIONTYPE::INNER) {
if (isOppositeCond(false, _tokenizer->isCPP(), cond1, cond2, _settings->library, true))
oppositeInnerConditionError(cond1, cond2);
} else {
if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true))
sameConditionAfterEarlyExitError(cond1, cond2);
}
} }
} }

View File

@ -1717,6 +1717,13 @@ private:
" if (x > 100) {}\n" " if (x > 100) {}\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition, second condition is always false\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition, second condition is always false\n", errout.str());
check("void f(int x) {\n"
" if (x > 100) { return; }\n"
" if (abc) {}\n"
" if (x > 100) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
} }
// clarify conditions with = and comparison // clarify conditions with = and comparison