From 36c7108a2821c477cf4d5eb9289281de8cd285ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 15 Sep 2009 20:46:47 +0200 Subject: [PATCH] Fixed #669 (possible style without --all + false positives) --- src/checkother.cpp | 5 ++++- test/testdivision.cpp | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index 6ebd0b761..036375b48 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -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 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) diff --git a/test/testdivision.cpp b/test/testdivision.cpp index 61621d8d8..5fe75b8d8 100644 --- a/test/testdivision.cpp +++ b/test/testdivision.cpp @@ -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)