Fixed #6574 (False positive oppositeInnerCondition - unknown variable)

This commit is contained in:
Daniel Marjamäki 2015-06-14 20:06:05 +02:00
parent 2edf133d07
commit 1e1ba6b4a9
2 changed files with 11 additions and 3 deletions

View File

@ -413,7 +413,7 @@ void CheckCondition::oppositeInnerCondition()
bool nonlocal = false; // nonlocal variable used in condition bool nonlocal = false; // nonlocal variable used in condition
std::set<unsigned int> vars; // variables used in condition std::set<unsigned int> vars; // variables used in condition
for (const Token *cond = scope->classDef; cond != scope->classDef->linkAt(1); cond = cond->next()) { for (const Token *cond = scope->classDef->linkAt(1); cond != scope->classDef; cond = cond->previous()) {
if (cond->varId()) { if (cond->varId()) {
vars.insert(cond->varId()); vars.insert(cond->varId());
const Variable *var = cond->variable(); const Variable *var = cond->variable();
@ -422,7 +422,7 @@ void CheckCondition::oppositeInnerCondition()
nonlocal |= (var && (var->isPointer() || var->isReference())); nonlocal |= (var && (var->isPointer() || var->isReference()));
} else if (cond->isName()) { } else if (cond->isName()) {
// varid is 0. this is possibly a nonlocal variable.. // varid is 0. this is possibly a nonlocal variable..
nonlocal |= (cond->astParent() && cond->astParent()->isConstOp()); nonlocal |= Token::Match(cond->astParent(), "%cop%|(");
} }
} }

View File

@ -1118,7 +1118,6 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo(int& i) {\n" check("void foo(int& i) {\n"
" i=6;\n" " i=6;\n"
"}\n" "}\n"
@ -1249,6 +1248,15 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #6574 - another fp when undeclared variable is used
check("void foo() {\n"
" if(i) {\n"
" i++;\n"
" if(!i) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// #5874 - array // #5874 - array
check("void testOppositeConditions2() {\n" check("void testOppositeConditions2() {\n"
" int array[2] = { 0, 0 };\n" " int array[2] = { 0, 0 };\n"