checkZeroDivisionOrUselessCondition: Fixed false positive when function is called and variable is passed to it
This commit is contained in:
parent
4973ae5183
commit
6bb6506ce2
|
@ -2177,13 +2177,19 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
|
|||
break;
|
||||
}
|
||||
}
|
||||
bool ifcondition = false;
|
||||
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
|
||||
if (tok2->varId() == varid)
|
||||
break;
|
||||
if (tok2->str() == "}")
|
||||
break;
|
||||
if (Token::Match(tok2, "%var% (") && tok2->str() != "if" && (var->isGlobal() || !tok2->function()))
|
||||
if (Token::Match(tok2, "%var% (")) {
|
||||
if (Token::simpleMatch(tok2, "if ("))
|
||||
ifcondition = true;
|
||||
else if (var->isGlobal() || !tok2->function())
|
||||
break;
|
||||
}
|
||||
if (ifcondition) {
|
||||
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) {
|
||||
zerodivcondError(tok2,tok);
|
||||
continue;
|
||||
|
@ -2193,6 +2199,7 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
|
|||
continue;
|
||||
}
|
||||
if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) {
|
||||
|
||||
zerodivcondError(tok2,tok);
|
||||
continue;
|
||||
}
|
||||
|
@ -2200,6 +2207,7 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
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"
|
||||
"void f() {\n"
|
||||
" int y = 17 / x;\n"
|
||||
|
|
Loading…
Reference in New Issue