Fixed #669 (possible style without --all + false positives)

This commit is contained in:
Daniel Marjamäki 2009-09-15 20:46:47 +02:00
parent 92b8593f59
commit 36c7108a28
2 changed files with 41 additions and 7 deletions

View File

@ -454,6 +454,9 @@ void CheckOther::invalidFunctionUsage()
void CheckOther::checkUnsignedDivision()
{
if (!_settings->_showAll || !_settings->_checkCodingStyle)
return;
// Check for "ivar / uvar" and "uvar / ivar"
std::map<std::string, char> varsign;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
@ -1307,7 +1310,7 @@ void CheckOther::udivError(const Token *tok)
void CheckOther::udivWarning(const Token *tok)
{
reportError(tok, Severity::possibleStyle, "udivWarning", "Warning: Division with signed and unsigned operators");
reportError(tok, Severity::possibleStyle, "udivWarning", "Division with signed and unsigned operators");
}
void CheckOther::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname)

View File

@ -36,7 +36,7 @@ public:
{ }
private:
void check(const char code[])
void check(const char code[], bool style = true, bool all = true)
{
// Tokenize..
Tokenizer tokenizer;
@ -47,8 +47,8 @@ private:
errout.str("");
Settings settings;
settings._showAll = true;
settings._checkCodingStyle = true;
settings._showAll = all;
settings._checkCodingStyle = style;
// Check for unsigned divisions..
CheckOther checkOther(&tokenizer, &settings, this);
@ -64,6 +64,7 @@ private:
TEST_CASE(division5);
TEST_CASE(division6);
TEST_CASE(division7);
TEST_CASE(division8);
}
void division1()
@ -74,7 +75,7 @@ private:
" unsigned int uvar = 2;\n"
" return ivar / uvar;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (possible style) Division with signed and unsigned operators\n", errout.str());
}
void division2()
@ -85,7 +86,7 @@ private:
" unsigned int uvar = 2;\n"
" return uvar / ivar;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (possible style) Division with signed and unsigned operators\n", errout.str());
}
void division3()
@ -98,7 +99,7 @@ private:
" u32 uvar = 2;\n"
" return uvar / ivar;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (possible style) Division with signed and unsigned operators\n", errout.str());
}
void division4()
@ -146,6 +147,36 @@ private:
);
ASSERT_EQUALS("[test.cpp:4]: (error) Unsigned division. The result will be wrong.\n", errout.str());
}
void division8()
{
check("void foo(int b)\n"
"{\n"
" if (b > 0)\n"
" {\n"
" unsigned int a;\n"
" unsigned int c = a / b;\n"
" }\n", false, true);
ASSERT_EQUALS("", errout.str());
check("void foo(int b)\n"
"{\n"
" if (b > 0)\n"
" {\n"
" unsigned int a;\n"
" unsigned int c = a / b;\n"
" }\n", true, false);
ASSERT_EQUALS("", errout.str());
check("void foo(int b)\n"
"{\n"
" if (b > 0)\n"
" {\n"
" unsigned int a;\n"
" unsigned int c = a / b;\n"
" }\n", true, true);
ASSERT_EQUALS("[test.cpp:6]: (possible style) Division with signed and unsigned operators\n", errout.str());
}
};
REGISTER_TEST(TestDivision)