From a55056c770f01b2ae74d2f3ab996c6a43358d5c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 22 Oct 2013 16:18:42 +0200 Subject: [PATCH] Fixed #5105 (false positive: (warning) Either the condition 'b!=0' is useless or there is division by zero) --- lib/checkother.cpp | 4 ++++ test/testother.cpp | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index e89b72599..d04a8ae1e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2267,6 +2267,10 @@ void CheckOther::checkZeroDivisionOrUselessCondition() zerodivcondError(tok2,divtok); else if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) zerodivcondError(tok2,divtok); + else if (Token::Match(tok2, "%var% (")) + // Todo: continue looking in condition unless variable might be + // changed by the function + break; else if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) zerodivcondError(tok2,divtok); } diff --git a/test/testother.cpp b/test/testother.cpp index 927792ba0..793c9c2de 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -578,6 +578,13 @@ private: CheckOther checkOther(&tokenizer, &settings, this); checkOther.checkZeroDivisionOrUselessCondition(); // don't crash } + + // #5105 - FP + check("int f(int a, int b) {\n" + " int r = a / b;\n" + " if (func(b)) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void nanInArithmeticExpression() {