Fixed #5128 (False positive zerodivcond 'a ? 1 / a : 0')

This commit is contained in:
Daniel Marjamäki 2013-10-30 16:51:00 +01:00
parent d50eef43a1
commit 06c5bd0daa
2 changed files with 34 additions and 0 deletions

View File

@ -2249,6 +2249,21 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
}
if (!divtok || use)
continue;
} else {
// Check if this division is guarded by a ?:
bool guard = false;
for (const Token *tok2 = divtok; tok2; tok2 = tok2->previous()) {
if (Token::Match(tok2, "[,;{}]"))
break;
if (Token::Match(tok2, "[?:]")) {
guard = true;
break;
}
if (tok2->str() == ")")
tok2 = tok2->link();
}
if (guard)
continue;
}
// Look for if condition

View File

@ -586,6 +586,25 @@ private:
" if (func(b)) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
// ?:
check("int f(int d) {\n"
" int r = (a?b:c) / d;\n"
" if (d == 0) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'd==0' is useless or there is division by zero at line 2.\n", errout.str());
check("int f(int a) {\n"
" int r = a ? 1 / a : 0;\n"
" if (a == 0) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f(int a) {\n"
" int r = (a == 0) ? 0 : 1 / a;\n"
" if (a == 0) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void nanInArithmeticExpression() {