checkZeroDivisionOrUselessCondition: Fixed false positive when function is called and variable is passed to it

This commit is contained in:
Daniel Marjamäki 2013-09-07 16:18:11 +02:00
parent 4973ae5183
commit 6bb6506ce2
2 changed files with 27 additions and 12 deletions

View File

@ -2177,24 +2177,32 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
break; break;
} }
} }
bool ifcondition = false;
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
if (tok2->varId() == varid) if (tok2->varId() == varid)
break; break;
if (tok2->str() == "}") if (tok2->str() == "}")
break; break;
if (Token::Match(tok2, "%var% (") && tok2->str() != "if" && (var->isGlobal() || !tok2->function())) if (Token::Match(tok2, "%var% (")) {
break; if (Token::simpleMatch(tok2, "if ("))
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) { ifcondition = true;
zerodivcondError(tok2,tok); else if (var->isGlobal() || !tok2->function())
continue; break;
} }
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) { if (ifcondition) {
zerodivcondError(tok2,tok); if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) {
continue; zerodivcondError(tok2,tok);
} continue;
if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) { }
zerodivcondError(tok2,tok); if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) {
continue; zerodivcondError(tok2,tok);
continue;
}
if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
}
} }
} }
} }

View File

@ -486,6 +486,13 @@ private:
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'x!=0' is useless or there is division by zero at line 4.\n", errout.str()); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'x!=0' is useless or there is division by zero at line 4.\n", errout.str());
} }
check("void do_something(int value);\n"
"void f(int x) {\n"
" int y = 17 / x;\n"
" do_something(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int x;\n" check("int x;\n"
"void f() {\n" "void f() {\n"
" int y = 17 / x;\n" " int y = 17 / x;\n"