Skip to content

Commit 36c7108

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#669 (possible style without --all + false positives)
1 parent 92b8593 commit 36c7108

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

src/checkother.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ void CheckOther::invalidFunctionUsage()
454454

455455
void CheckOther::checkUnsignedDivision()
456456
{
457+
if (!_settings->_showAll || !_settings->_checkCodingStyle)
458+
return;
459+
457460
// Check for "ivar / uvar" and "uvar / ivar"
458461
std::map<std::string, char> varsign;
459462
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
@@ -1307,7 +1310,7 @@ void CheckOther::udivError(const Token *tok)
13071310

13081311
void CheckOther::udivWarning(const Token *tok)
13091312
{
1310-
reportError(tok, Severity::possibleStyle, "udivWarning", "Warning: Division with signed and unsigned operators");
1313+
reportError(tok, Severity::possibleStyle, "udivWarning", "Division with signed and unsigned operators");
13111314
}
13121315

13131316
void CheckOther::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname)

test/testdivision.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TestDivision : public TestFixture
3636
{ }
3737

3838
private:
39-
void check(const char code[])
39+
void check(const char code[], bool style = true, bool all = true)
4040
{
4141
// Tokenize..
4242
Tokenizer tokenizer;
@@ -47,8 +47,8 @@ class TestDivision : public TestFixture
4747
errout.str("");
4848

4949
Settings settings;
50-
settings._showAll = true;
51-
settings._checkCodingStyle = true;
50+
settings._showAll = all;
51+
settings._checkCodingStyle = style;
5252

5353
// Check for unsigned divisions..
5454
CheckOther checkOther(&tokenizer, &settings, this);
@@ -64,6 +64,7 @@ class TestDivision : public TestFixture
6464
TEST_CASE(division5);
6565
TEST_CASE(division6);
6666
TEST_CASE(division7);
67+
TEST_CASE(division8);
6768
}
6869

6970
void division1()
@@ -74,7 +75,7 @@ class TestDivision : public TestFixture
7475
" unsigned int uvar = 2;\n"
7576
" return ivar / uvar;\n"
7677
"}\n");
77-
ASSERT_EQUALS("[test.cpp:5]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
78+
ASSERT_EQUALS("[test.cpp:5]: (possible style) Division with signed and unsigned operators\n", errout.str());
7879
}
7980

8081
void division2()
@@ -85,7 +86,7 @@ class TestDivision : public TestFixture
8586
" unsigned int uvar = 2;\n"
8687
" return uvar / ivar;\n"
8788
"}\n");
88-
ASSERT_EQUALS("[test.cpp:5]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
89+
ASSERT_EQUALS("[test.cpp:5]: (possible style) Division with signed and unsigned operators\n", errout.str());
8990
}
9091

9192
void division3()
@@ -98,7 +99,7 @@ class TestDivision : public TestFixture
9899
" u32 uvar = 2;\n"
99100
" return uvar / ivar;\n"
100101
"}\n");
101-
ASSERT_EQUALS("[test.cpp:7]: (possible style) Warning: Division with signed and unsigned operators\n", errout.str());
102+
ASSERT_EQUALS("[test.cpp:7]: (possible style) Division with signed and unsigned operators\n", errout.str());
102103
}
103104

104105
void division4()
@@ -146,6 +147,36 @@ class TestDivision : public TestFixture
146147
);
147148
ASSERT_EQUALS("[test.cpp:4]: (error) Unsigned division. The result will be wrong.\n", errout.str());
148149
}
150+
151+
void division8()
152+
{
153+
check("void foo(int b)\n"
154+
"{\n"
155+
" if (b > 0)\n"
156+
" {\n"
157+
" unsigned int a;\n"
158+
" unsigned int c = a / b;\n"
159+
" }\n", false, true);
160+
ASSERT_EQUALS("", errout.str());
161+
162+
check("void foo(int b)\n"
163+
"{\n"
164+
" if (b > 0)\n"
165+
" {\n"
166+
" unsigned int a;\n"
167+
" unsigned int c = a / b;\n"
168+
" }\n", true, false);
169+
ASSERT_EQUALS("", errout.str());
170+
171+
check("void foo(int b)\n"
172+
"{\n"
173+
" if (b > 0)\n"
174+
" {\n"
175+
" unsigned int a;\n"
176+
" unsigned int c = a / b;\n"
177+
" }\n", true, true);
178+
ASSERT_EQUALS("[test.cpp:6]: (possible style) Division with signed and unsigned operators\n", errout.str());
179+
}
149180
};
150181

151182
REGISTER_TEST(TestDivision)

0 commit comments

Comments
 (0)