Fixed #9335 (FP constStatement "Found a statement that begins with string constant")
This commit is contained in:
parent
ff36ebcff2
commit
676a837af6
|
@ -1569,7 +1569,7 @@ void CheckOther::checkIncompleteStatement()
|
||||||
if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") &&
|
if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") &&
|
||||||
!Token::Match(tok->previous(), ";|}|{ %any% ;"))
|
!Token::Match(tok->previous(), ";|}|{ %any% ;"))
|
||||||
continue;
|
continue;
|
||||||
// Skipe statement expressions
|
// Skip statement expressions
|
||||||
if (Token::simpleMatch(rtok, "; } )"))
|
if (Token::simpleMatch(rtok, "; } )"))
|
||||||
continue;
|
continue;
|
||||||
if (!isConstStatement(tok))
|
if (!isConstStatement(tok))
|
||||||
|
@ -1587,6 +1587,10 @@ void CheckOther::checkIncompleteStatement()
|
||||||
|
|
||||||
void CheckOther::constStatementError(const Token *tok, const std::string &type, bool inconclusive)
|
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;
|
std::string msg;
|
||||||
if (Token::simpleMatch(tok, "=="))
|
if (Token::simpleMatch(tok, "=="))
|
||||||
msg = "Found suspicious equality comparison. Did you intend to assign a value instead?";
|
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() + "'";
|
msg = "Found suspicious operator '" + tok->str() + "'";
|
||||||
else if (Token::Match(tok, "%var%"))
|
else if (Token::Match(tok, "%var%"))
|
||||||
msg = "Unused variable value '" + tok->str() + "'";
|
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.";
|
msg = "Redundant code: Found a statement that begins with " + type + " constant.";
|
||||||
|
else
|
||||||
|
return; // Strange!
|
||||||
reportError(tok, Severity::warning, "constStatement", msg, CWE398, inconclusive);
|
reportError(tok, Severity::warning, "constStatement", msg, CWE398, inconclusive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,6 +68,7 @@ private:
|
||||||
TEST_CASE(test4);
|
TEST_CASE(test4);
|
||||||
TEST_CASE(test5);
|
TEST_CASE(test5);
|
||||||
TEST_CASE(test6);
|
TEST_CASE(test6);
|
||||||
|
TEST_CASE(test7);
|
||||||
TEST_CASE(test_numeric);
|
TEST_CASE(test_numeric);
|
||||||
TEST_CASE(void0); // #6327: No fp for statement "(void)0;"
|
TEST_CASE(void0); // #6327: No fp for statement "(void)0;"
|
||||||
TEST_CASE(intarray);
|
TEST_CASE(intarray);
|
||||||
|
@ -150,14 +151,28 @@ 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() {
|
void test_numeric() {
|
||||||
check("struct P\n"
|
check("struct P {\n"
|
||||||
"{\n"
|
|
||||||
" double a;\n"
|
" double a;\n"
|
||||||
" double b;\n"
|
" double b;\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"void f()\n"
|
"void f() {\n"
|
||||||
"{\n"
|
|
||||||
" const P values[2] =\n"
|
" const P values[2] =\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" { 346.1,114.1 }, { 347.1,111.1 }\n"
|
" { 346.1,114.1 }, { 347.1,111.1 }\n"
|
||||||
|
@ -343,8 +358,8 @@ private:
|
||||||
"}\n", true);
|
"}\n", true);
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant code: Found a statement that begins with numeric constant.\n"
|
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: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: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 string 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:6]: (warning, inconclusive) Found suspicious operator '!'\n"
|
||||||
"[test.cpp:7]: (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());
|
"[test.cpp:9]: (warning, inconclusive) Found suspicious operator '~'\n", errout.str());
|
||||||
|
|
Loading…
Reference in New Issue