Skip to content

Commit 6d3b47c

Browse files
authored
fixed #13506 - fixed tokenizing of +-prefixed numbers in conditions (#7230)
1 parent 10bab82 commit 6d3b47c

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

Diff for: lib/tokenize.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -3626,9 +3626,11 @@ void Tokenizer::concatenateNegativeNumberAndAnyPositive()
36263626
while (tok->str() != ">" && tok->next() && tok->strAt(1) == "+" && (!Token::Match(tok->tokAt(2), "%name% (|;") || Token::Match(tok, "%op%")))
36273627
tok->deleteNext();
36283628

3629-
if (Token::Match(tok->next(), "- %num%")) {
3629+
if (Token::Match(tok->next(), "+|- %num%")) {
3630+
// cppcheck-suppress redundantCopyLocalConst - cannot make it a reference because it is deleted afterwards
3631+
std::string prefix = tok->strAt(1);
36303632
tok->deleteNext();
3631-
tok->next()->str("-" + tok->strAt(1));
3633+
tok->next()->str(prefix + tok->strAt(1));
36323634
}
36333635
}
36343636
}

Diff for: test/testcondition.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -6354,6 +6354,14 @@ class TestCondition : public TestFixture {
63546354
"[test.cpp:3]: (style) Condition 'f>1.00' is always false\n",
63556355
"",
63566356
errout_str());
6357+
6358+
check("void foo() {\n" // #13506
6359+
" float nf = -1.0;\n"
6360+
" if (nf > +1.0) {}\n"
6361+
"}\n");
6362+
ASSERT_EQUALS(
6363+
"[test.cpp:3]: (style) Condition 'nf>+1.0' is always false\n",
6364+
errout_str());
63576365
}
63586366
};
63596367

Diff for: test/testother.cpp

+41-7
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ class TestOther : public TestFixture {
302302
TEST_CASE(knownPointerToBool);
303303
TEST_CASE(iterateByValue);
304304

305-
TEST_CASE(alwaysTrueFloating);
305+
TEST_CASE(knownConditionFloating);
306+
TEST_CASE(knownConditionPrefixed);
306307
}
307308

308309
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
@@ -12888,7 +12889,7 @@ class TestOther : public TestFixture {
1288812889
errout_str());
1288912890
}
1289012891

12891-
void alwaysTrueFloating()
12892+
void knownConditionFloating()
1289212893
{
1289312894
check("void foo() {\n" // #11200
1289412895
" float f = 1.0;\n"
@@ -12905,9 +12906,16 @@ class TestOther : public TestFixture {
1290512906
" float f = 1.0;\n"
1290612907
" if (f > +1.0) {}\n"
1290712908
"}\n");
12908-
TODO_ASSERT_EQUALS(
12909+
ASSERT_EQUALS(
1290912910
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'f > +1.0' is always false.\n",
12910-
"",
12911+
errout_str());
12912+
12913+
check("void foo() {\n"
12914+
" float f = 1.0;\n"
12915+
" if (f < +1.0) {}\n"
12916+
"}\n");
12917+
ASSERT_EQUALS(
12918+
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'f < 1.0' is always false.\n",
1291112919
errout_str());
1291212920

1291312921
check("void foo() {\n" // #11200
@@ -12925,9 +12933,16 @@ class TestOther : public TestFixture {
1292512933
" float pf = +1.0;\n"
1292612934
" if (pf > +1.0) {}\n"
1292712935
"}\n");
12928-
TODO_ASSERT_EQUALS(
12936+
ASSERT_EQUALS(
1292912937
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'pf > +1.0' is always false.\n",
12930-
"",
12938+
errout_str());
12939+
12940+
check("void foo() {\n"
12941+
" float pf = +1.0;\n"
12942+
" if (pf < +1.0) {}\n"
12943+
"}\n");
12944+
ASSERT_EQUALS(
12945+
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'pf < 1.0' is always false.\n",
1293112946
errout_str());
1293212947

1293312948
check("void foo() {\n" // #11200
@@ -12941,7 +12956,7 @@ class TestOther : public TestFixture {
1294112956
"[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'nf > -1.0' is always false.\n",
1294212957
errout_str());
1294312958

12944-
check("void foo() {\n" // #13506
12959+
check("void foo() {\n" // #13508
1294512960
" float nf = -1.0;\n"
1294612961
" if (nf > +1.0) {}\n"
1294712962
"}\n");
@@ -12984,6 +12999,25 @@ class TestOther : public TestFixture {
1298412999
"",
1298513000
errout_str());
1298613001
}
13002+
13003+
void knownConditionPrefixed()
13004+
{
13005+
check("void foo() {\n"
13006+
" int i = 1;\n"
13007+
" if (i < +1) {}\n"
13008+
"}\n");
13009+
ASSERT_EQUALS(
13010+
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i < 1' is always false.\n",
13011+
errout_str());
13012+
13013+
check("void foo() {\n" // #13506
13014+
" int i = 1;\n"
13015+
" if (i > +1) {}\n"
13016+
"}\n");
13017+
ASSERT_EQUALS(
13018+
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i > +1' is always false.\n",
13019+
errout_str());
13020+
}
1298713021
};
1298813022

1298913023
REGISTER_TEST(TestOther)

0 commit comments

Comments
 (0)