From 676a837af629bbfb115fc5d6d851e824e4a4d9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 28 Nov 2019 17:49:07 +0100 Subject: [PATCH] Fixed #9335 (FP constStatement "Found a statement that begins with string constant") --- lib/checkother.cpp | 12 ++++++++-- test/testincompletestatement.cpp | 39 ++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2b183a15d..1aa4436a7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1569,7 +1569,7 @@ void CheckOther::checkIncompleteStatement() if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") && !Token::Match(tok->previous(), ";|}|{ %any% ;")) continue; - // Skipe statement expressions + // Skip statement expressions if (Token::simpleMatch(rtok, "; } )")) continue; if (!isConstStatement(tok)) @@ -1587,6 +1587,10 @@ void CheckOther::checkIncompleteStatement() void CheckOther::constStatementError(const Token *tok, const std::string &type, bool inconclusive) { + const Token *valueTok = tok; + while (valueTok && valueTok->isCast()) + valueTok = valueTok->astOperand2() ? valueTok->astOperand2() : valueTok->astOperand1(); + std::string msg; if (Token::simpleMatch(tok, "==")) msg = "Found suspicious equality comparison. Did you intend to assign a value instead?"; @@ -1594,8 +1598,12 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, msg = "Found suspicious operator '" + tok->str() + "'"; else if (Token::Match(tok, "%var%")) msg = "Unused variable value '" + tok->str() + "'"; - else + else if (Token::Match(valueTok, "%str%|%num%")) + msg = "Redundant code: Found a statement that begins with " + std::string(valueTok->isNumber() ? "numeric" : "string") + " constant."; + else if (!tok) msg = "Redundant code: Found a statement that begins with " + type + " constant."; + else + return; // Strange! reportError(tok, Severity::warning, "constStatement", msg, CWE398, inconclusive); } diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index f0dc7f87c..71d2045d6 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -68,6 +68,7 @@ private: TEST_CASE(test4); TEST_CASE(test5); TEST_CASE(test6); + TEST_CASE(test7); TEST_CASE(test_numeric); TEST_CASE(void0); // #6327: No fp for statement "(void)0;" TEST_CASE(intarray); @@ -150,18 +151,32 @@ private: "}"); } + void test7() { // #9335 + check("namespace { std::string S = \"\"; }\n" + "\n" + "class C {\n" + "public:\n" + " explicit C(const std::string& s);\n" + "};\n" + "\n" + "void f() {\n" + " for (C c(S); ; ) {\n" + " (void)c;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void test_numeric() { - check("struct P\n" - "{\n" - "double a;\n" - "double b;\n" - "};\n" - "void f()\n" - "{\n" - "const P values[2] =\n" - "{\n" - "{ 346.1,114.1 }, { 347.1,111.1 }\n" + check("struct P {\n" + " double a;\n" + " double b;\n" "};\n" + "void f() {\n" + " const P values[2] =\n" + " {\n" + " { 346.1,114.1 }, { 347.1,111.1 }\n" + " };\n" "}"); ASSERT_EQUALS("", errout.str()); @@ -343,8 +358,8 @@ private: "}\n", true); ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:3]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" - "[test.cpp:4]: (warning) Redundant code: Found a statement that begins with string constant.\n" - "[test.cpp:5]: (warning) Redundant code: Found a statement that begins with string constant.\n" + "[test.cpp:4]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" + "[test.cpp:5]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:6]: (warning, inconclusive) Found suspicious operator '!'\n" "[test.cpp:7]: (warning, inconclusive) Found suspicious operator '!'\n" "[test.cpp:9]: (warning, inconclusive) Found suspicious operator '~'\n", errout.str());