Skip to content

Commit 1f2bbf5

Browse files
committed
Adjust error messages
1 parent 6fb5cb0 commit 1f2bbf5

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

lib/checkother.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,36 +2419,32 @@ void CheckOtherImpl::checkIncompleteStatement()
24192419

24202420
void CheckOtherImpl::constStatementError(const Token *tok, const std::string &type, bool inconclusive)
24212421
{
2422-
const Token *valueTok = tok;
2423-
while (valueTok && valueTok->isCast())
2424-
valueTok = valueTok->astOperand2() ? valueTok->astOperand2() : valueTok->astOperand1();
2425-
24262422
std::string msg;
24272423
if (Token::simpleMatch(tok, "=="))
24282424
msg = "Found suspicious equality comparison. Did you intend to assign a value instead?";
24292425
else if (Token::Match(tok, ",|!|~|%cop%"))
24302426
msg = "Found suspicious operator '" + tok->str() + "', result is not used.";
24312427
else if (Token::Match(tok, "%var%"))
24322428
msg = "Unused variable value '" + tok->str() + "'";
2433-
else if (isConstant(valueTok)) {
2429+
else if (isConstant(tok)) {
24342430
std::string typeStr("string");
2435-
if (valueTok->isNumber())
2431+
if (tok->isNumber())
24362432
typeStr = "numeric";
2437-
else if (valueTok->isBoolean())
2433+
else if (tok->isBoolean())
24382434
typeStr = "bool";
2439-
else if (valueTok->tokType() == Token::eChar)
2435+
else if (tok->tokType() == Token::eChar)
24402436
typeStr = "character";
2441-
else if (isNullOperand(valueTok))
2442-
typeStr = "NULL";
2443-
else if (valueTok->isEnumerator())
2437+
else if (isNullOperand(tok))
2438+
typeStr = "null";
2439+
else if (tok->isEnumerator())
24442440
typeStr = "enumerator";
24452441
msg = "Redundant code: Found a statement that begins with " + typeStr + " constant.";
24462442
}
24472443
else if (!tok)
24482444
msg = "Redundant code: Found a statement that begins with " + type + " constant.";
24492445
else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp) {
24502446
msg = "Redundant code: Found unused cast ";
2451-
msg += valueTok ? "of expression '" + valueTok->expressionString() + "'." : "expression.";
2447+
msg += tok ? "in expression '" + tok->expressionString() + "'." : "expression.";
24522448
}
24532449
else if (tok->str() == "?" && tok->tokType() == Token::Type::eExtendedOp)
24542450
msg = "Redundant code: Found unused result of ternary operator.";

test/testincompletestatement.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ class TestIncompleteStatement : public TestFixture {
187187
ASSERT_EQUALS("", errout_str());
188188

189189
check("void f() { (void*)0; }");
190-
ASSERT_EQUALS("[test.cpp:1:12]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n", errout_str());
190+
ASSERT_EQUALS("[test.cpp:1:12]: (warning) Redundant code: Found unused cast in expression '(void*)0'. [constStatement]\n", errout_str());
191191

192192
check("void f() {\n" // #13148
193193
" static_cast<void>(1);\n"
194194
" static_cast<void>(nullptr);\n"
195195
" (void)NULL;\n"
196196
"}\n");
197-
ASSERT_EQUALS("[test.cpp:2:22]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
198-
"[test.cpp:3:22]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n"
199-
"[test.cpp:4:5]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n",
197+
ASSERT_EQUALS("[test.cpp:2:22]: (warning) Redundant code: Found unused cast in expression 'static_cast<void>(1)'. [constStatement]\n"
198+
"[test.cpp:3:22]: (warning) Redundant code: Found unused cast in expression 'static_cast<void>(nullptr)'. [constStatement]\n"
199+
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast in expression '(void)NULL'. [constStatement]\n",
200200
errout_str());
201201

202202
check("#define X 0\n"
@@ -448,11 +448,11 @@ class TestIncompleteStatement : public TestFixture {
448448
"}\n", dinit(CheckOptions, $.inconclusive = true));
449449
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
450450
"[test.cpp:3:6]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
451-
"[test.cpp:4:5]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
452-
"[test.cpp:5:6]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
451+
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast in expression '(char)1'. [constStatement]\n"
452+
"[test.cpp:5:6]: (warning) Redundant code: Found unused cast in expression '(char)1'. [constStatement]\n"
453453
"[test.cpp:6:5]: (warning, inconclusive) Found suspicious operator '!', result is not used. [constStatement]\n"
454454
"[test.cpp:7:6]: (warning, inconclusive) Found suspicious operator '!', result is not used. [constStatement]\n"
455-
"[test.cpp:8:5]: (warning) Redundant code: Found unused cast of expression '!x'. [constStatement]\n"
455+
"[test.cpp:8:5]: (warning) Redundant code: Found unused cast in expression '(unsigned int)!x'. [constStatement]\n"
456456
"[test.cpp:9:5]: (warning, inconclusive) Found suspicious operator '~', result is not used. [constStatement]\n",
457457
errout_str());
458458

@@ -463,7 +463,7 @@ class TestIncompleteStatement : public TestFixture {
463463
ASSERT_EQUALS("", errout_str());
464464

465465
check("void f(int x) { static_cast<unsigned>(x); }");
466-
ASSERT_EQUALS("[test.cpp:1:38]: (warning) Redundant code: Found unused cast of expression 'x'. [constStatement]\n", errout_str());
466+
ASSERT_EQUALS("[test.cpp:1:38]: (warning) Redundant code: Found unused cast in expression 'static_cast<unsigned int>(x)'. [constStatement]\n", errout_str());
467467

468468
check("void f(int x, int* p) {\n"
469469
" static_cast<void>(x);\n"
@@ -481,9 +481,9 @@ class TestIncompleteStatement : public TestFixture {
481481
" static_cast<float>((char)i);\n"
482482
" (char)static_cast<float>(i);\n"
483483
"}\n");
484-
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n"
485-
"[test.cpp:3:23]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n"
486-
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n",
484+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast in expression '(float)(char)i'. [constStatement]\n"
485+
"[test.cpp:3:23]: (warning) Redundant code: Found unused cast in expression 'static_cast<float>((char)i)'. [constStatement]\n"
486+
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast in expression '(char)static_cast<float>(i)'. [constStatement]\n",
487487
errout_str());
488488

489489
check("namespace M {\n"
@@ -492,7 +492,7 @@ class TestIncompleteStatement : public TestFixture {
492492
"void f(int i) {\n"
493493
" (M::N::T)i;\n"
494494
"}\n");
495-
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n", errout_str());
495+
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast in expression '(char)i'. [constStatement]\n", errout_str());
496496

497497
check("void f(int (g)(int a, int b)) {\n" // #10873
498498
" int p = 0, q = 1;\n"
@@ -544,7 +544,7 @@ class TestIncompleteStatement : public TestFixture {
544544
" for (L\"y\"; ;) {}\n"
545545
"}\n");
546546
ASSERT_EQUALS("[test.cpp:2:10]: (warning) Unused variable value 'i' [constStatement]\n"
547-
"[test.cpp:3:10]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n"
547+
"[test.cpp:3:10]: (warning) Redundant code: Found unused cast in expression '(long)i'. [constStatement]\n"
548548
"[test.cpp:4:10]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
549549
"[test.cpp:5:10]: (warning) Redundant code: Found a statement that begins with bool constant. [constStatement]\n"
550550
"[test.cpp:6:10]: (warning) Redundant code: Found a statement that begins with character constant. [constStatement]\n"
@@ -712,8 +712,8 @@ class TestIncompleteStatement : public TestFixture {
712712
" NULL;\n"
713713
" nullptr;\n"
714714
"}\n");
715-
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n"
716-
"[test.cpp:3:5]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n",
715+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with null constant. [constStatement]\n"
716+
"[test.cpp:3:5]: (warning) Redundant code: Found a statement that begins with null constant. [constStatement]\n",
717717
errout_str());
718718

719719
check("struct S { int i; };\n" // #6504

test/testother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,7 +3914,7 @@ class TestOther : public TestFixture {
39143914
" (void)(true);\n"
39153915
" if (r) {}\n"
39163916
"}\n");
3917-
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with bool constant. [constStatement]\n"
3917+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast in expression '(void)(true)'. [constStatement]\n"
39183918
"[test.cpp:1:13]: (style) Parameter 'r' can be declared as reference to const [constParameterReference]\n",
39193919
errout_str());
39203920

@@ -7014,7 +7014,7 @@ class TestOther : public TestFixture {
70147014
" std::pair<int, int>(1, 2);\n"
70157015
" (void)0;\n"
70167016
"}\n");
7017-
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
7017+
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast in expression '(void)0'. [constStatement]\n"
70187018
"[test.cpp:2:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
70197019
"[test.cpp:3:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
70207020
"[test.cpp:4:10]: (style) Instance of 'std::pair' object is destroyed immediately. [unusedScopedObject]\n",

0 commit comments

Comments
 (0)