diff --git a/lib/checkother.cpp b/lib/checkother.cpp index afa89c21d..cc97f60d8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2177,24 +2177,32 @@ 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())) - break; - if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) { - zerodivcondError(tok2,tok); - continue; + if (Token::Match(tok2, "%var% (")) { + if (Token::simpleMatch(tok2, "if (")) + ifcondition = true; + else if (var->isGlobal() || !tok2->function()) + break; } - if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) { - zerodivcondError(tok2,tok); - continue; - } - if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) { - zerodivcondError(tok2,tok); - continue; + if (ifcondition) { + if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) { + zerodivcondError(tok2,tok); + continue; + } + if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) { + zerodivcondError(tok2,tok); + continue; + } + if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) { + + zerodivcondError(tok2,tok); + continue; + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index 99f6fbdfc..d8b138bd3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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"