Fix #3516 (inaccurate warning: "An unsigned variable will always be positive" (unsigned n; if (n >=0);))

http://sourceforge.net/apps/trac/cppcheck/ticket/3516
This commit is contained in:
Reijo Tomperi 2012-01-14 12:50:09 +02:00
parent 6a248c35b5
commit dd18f595b1
2 changed files with 10 additions and 13 deletions

View File

@ -3066,15 +3066,12 @@ void CheckOther::unsignedPositiveError(const Token *tok, const std::string &varn
{ {
if (inconclusive) { if (inconclusive) {
reportInconclusiveError(tok, Severity::style, "unsignedPositive", reportInconclusiveError(tok, Severity::style, "unsignedPositive",
"Checking if unsigned variable '" + varname + "' is positive is always true. This might be a false warning.\n" "An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it. This might be a false warning.\n"
"Checking if unsigned variable '" + varname + "' is positive is always true. " "An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it. "
"An unsigned variable will always be positive so it is either pointless or " "It's not known if the used constant is a "
"an error to check if it is. It's not known if the used constant is a "
"template parameter or not and therefore this message might be a false warning"); "template parameter or not and therefore this message might be a false warning");
} else { } else {
reportError(tok, Severity::style, "unsignedPositive", reportError(tok, Severity::style, "unsignedPositive",
"Checking if unsigned variable '" + varname + "' is positive is always true.\n" "An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it.");
"An unsigned variable will always be positive so it is either pointless or "
"an error to check if it is.");
} }
} }

View File

@ -4153,14 +4153,14 @@ private:
" for(unsigned char i = 10; i >= 0; i--)" " for(unsigned char i = 10; i >= 0; i--)"
" printf(\"%u\", i);\n" " printf(\"%u\", i);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Checking if unsigned variable 'i' is positive is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) An unsigned variable 'i' can't be negative so it is unnecessary to test it.\n", errout.str());
check_signOfUnsignedVariable( check_signOfUnsignedVariable(
"void foo(bool b) {\n" "void foo(bool b) {\n"
" for(unsigned int i = 10; b || i >= 0; i--)" " for(unsigned int i = 10; b || i >= 0; i--)"
" printf(\"%u\", i);\n" " printf(\"%u\", i);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Checking if unsigned variable 'i' is positive is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) An unsigned variable 'i' can't be negative so it is unnecessary to test it.\n", errout.str());
check_signOfUnsignedVariable( check_signOfUnsignedVariable(
"bool foo(unsigned int x) {\n" "bool foo(unsigned int x) {\n"
@ -4200,7 +4200,7 @@ private:
" return true;\n" " return true;\n"
" return false;\n" " return false;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Checking if unsigned variable 'x' is positive is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) An unsigned variable 'x' can't be negative so it is unnecessary to test it.\n", errout.str());
check_signOfUnsignedVariable( check_signOfUnsignedVariable(
"bool foo(int x) {\n" "bool foo(int x) {\n"
@ -4249,7 +4249,7 @@ private:
" return true;\n" " return true;\n"
" return false;\n" " return false;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Checking if unsigned variable 'x' is positive is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) An unsigned variable 'x' can't be negative so it is unnecessary to test it.\n", errout.str());
check_signOfUnsignedVariable( check_signOfUnsignedVariable(
"bool foo(int x, bool y) {\n" "bool foo(int x, bool y) {\n"
@ -4298,7 +4298,7 @@ private:
" return true;\n" " return true;\n"
" return false;\n" " return false;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Checking if unsigned variable 'x' is positive is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) An unsigned variable 'x' can't be negative so it is unnecessary to test it.\n", errout.str());
check_signOfUnsignedVariable( check_signOfUnsignedVariable(
"bool foo(int x, bool y) {\n" "bool foo(int x, bool y) {\n"
@ -4347,7 +4347,7 @@ private:
" return true;\n" " return true;\n"
" return false;\n" " return false;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Checking if unsigned variable 'x' is positive is always true.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) An unsigned variable 'x' can't be negative so it is unnecessary to test it.\n", errout.str());
check_signOfUnsignedVariable( check_signOfUnsignedVariable(
"bool foo(int x, bool y) {\n" "bool foo(int x, bool y) {\n"