Skip to content

Commit 9b4b8b2

Browse files
committed
Ticket cppcheck-opensource#2238 (Improve postincrement warning message)
Have a proper short message for post ++/-- operators. Pre ++/-- operators are usually more efficient for non-primitive types.
1 parent 87b69a1 commit 9b4b8b2

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

lib/checkpostfixoperator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,10 @@ void CheckPostfixOperator::postfixOperator()
9696

9797
void CheckPostfixOperator::postfixOperatorError(const Token *tok)
9898
{
99-
reportError(tok, Severity::performance, "postfixOperator", "You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators");
99+
reportError(tok, Severity::performance, "postfixOperator",
100+
"Prefer prefix ++/-- operators for non-primitive types.\n "
101+
"Pre-increment/decrement can be more efficient than "
102+
"post-increment/decrement. Post-increment/decrement usually "
103+
"involves keeping a copy of the previous value around and "
104+
"adds a little extra code.");
100105
}

test/testpostfixoperator.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class TestPostfixOperator : public TestFixture
101101
" std::cout << k << std::endl;\n"
102102
" return 0;\n"
103103
"}\n");
104-
ASSERT_EQUALS("[test.cpp:7]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
104+
ASSERT_EQUALS("[test.cpp:7]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
105105

106106
check("\n"
107107
"#include <iostream>\n"
@@ -116,7 +116,7 @@ class TestPostfixOperator : public TestFixture
116116
" std::cout << k << std::endl;\n"
117117
" return 0;\n"
118118
"}\n");
119-
ASSERT_EQUALS("[test.cpp:8]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
119+
ASSERT_EQUALS("[test.cpp:8]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
120120

121121
check("\n"
122122
"#include <iostream>\n"
@@ -132,7 +132,7 @@ class TestPostfixOperator : public TestFixture
132132
" std::cout << k << std::endl;\n"
133133
" return 0;\n"
134134
"}\n");
135-
ASSERT_EQUALS("[test.cpp:10]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
135+
ASSERT_EQUALS("[test.cpp:10]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
136136

137137

138138
check("\n"
@@ -146,7 +146,7 @@ class TestPostfixOperator : public TestFixture
146146
" std::cout << k << std::endl;\n"
147147
" return 0;\n"
148148
"}\n");
149-
ASSERT_EQUALS("[test.cpp:7]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
149+
ASSERT_EQUALS("[test.cpp:7]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
150150

151151
check("\n"
152152
"#include <iostream>\n"
@@ -199,7 +199,7 @@ class TestPostfixOperator : public TestFixture
199199
" }\n"
200200
" return 0;\n"
201201
"}\n");
202-
ASSERT_EQUALS("[test.cpp:6]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
202+
ASSERT_EQUALS("[test.cpp:6]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
203203

204204
check("\n"
205205
"#include <iostream>\n"
@@ -223,7 +223,7 @@ class TestPostfixOperator : public TestFixture
223223
" }\n"
224224
" return 0;\n"
225225
"}\n");
226-
ASSERT_EQUALS("[test.cpp:6]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
226+
ASSERT_EQUALS("[test.cpp:6]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
227227

228228
check("\n"
229229
"#include <iostream>\n"
@@ -264,7 +264,7 @@ class TestPostfixOperator : public TestFixture
264264
" std::cout << k-- << std::endl;\n"
265265
" return 0;\n"
266266
"}\n");
267-
ASSERT_EQUALS("[test.cpp:8]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
267+
ASSERT_EQUALS("[test.cpp:8]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
268268

269269
check("\n"
270270
"#include <iostream>\n"
@@ -311,7 +311,7 @@ class TestPostfixOperator : public TestFixture
311311
" v.clear();\n"
312312
" return 0;\n"
313313
"}\n");
314-
ASSERT_EQUALS("[test.cpp:8]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
314+
ASSERT_EQUALS("[test.cpp:8]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
315315

316316
check("\n"
317317
"#include <iostream>\n"
@@ -328,7 +328,7 @@ class TestPostfixOperator : public TestFixture
328328
" }\n"
329329
" return 0;\n"
330330
"}\n");
331-
ASSERT_EQUALS("[test.cpp:12]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
331+
ASSERT_EQUALS("[test.cpp:12]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
332332

333333
check("\n"
334334
"#include <iostream>\n"
@@ -344,7 +344,7 @@ class TestPostfixOperator : public TestFixture
344344
" }\n"
345345
" return 0;\n"
346346
"}\n");
347-
ASSERT_EQUALS("[test.cpp:11]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
347+
ASSERT_EQUALS("[test.cpp:11]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
348348

349349
check("\n"
350350
"#include <iostream>\n"
@@ -361,7 +361,7 @@ class TestPostfixOperator : public TestFixture
361361
" }\n"
362362
" return 0;\n"
363363
"}\n");
364-
ASSERT_EQUALS("[test.cpp:12]: (performance) You should use ++ and -- as prefix whenever possible as these are more efficient than postfix operators\n", errout.str());
364+
ASSERT_EQUALS("[test.cpp:12]: (performance) Prefer prefix ++/-- operators for non-primitive types.\n", errout.str());
365365

366366
}
367367

0 commit comments

Comments
 (0)