Fix #10873 FP constStatement with extra parentheses in function call (#3911)

* Fix #10873 FP constStatement with extra parentheses in function call

* Format
This commit is contained in:
chrchr-github 2022-03-17 12:47:27 +01:00 committed by GitHub
parent e073860e62
commit 97ce569859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -1759,10 +1759,8 @@ static bool isConstStatement(const Token *tok, bool cpp)
return true;
if (isCPPCast(tok))
return isWithoutSideEffects(cpp, tok) && isConstStatement(tok->astOperand2(), cpp);
else if (tok->isCast())
else if (tok->isCast() && tok->next() && tok->next()->isStandardType())
return isWithoutSideEffects(cpp, tok->astOperand1()) && isConstStatement(tok->astOperand1(), cpp);
if (Token::Match(tok, "( %type%"))
return isConstStatement(tok->astOperand1(), cpp);
if (Token::simpleMatch(tok, "."))
return isConstStatement(tok->astOperand2(), cpp);
if (Token::simpleMatch(tok, ",")) // warn about const statement on rhs at the top level

View File

@ -170,7 +170,7 @@ private:
" (void)c;\n"
" }\n"
"}");
TODO_ASSERT_EQUALS("", "[test.cpp:9]: (debug) constStatementError not handled.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
void test_numeric() {
@ -408,6 +408,25 @@ private:
"[test.cpp:4]: (warning) Redundant code: Found unused cast of expression 'i'.\n",
errout.str());
check("namespace M {\n"
" namespace N { typedef char T; }\n"
"}\n"
"void f(int i) {\n"
" (M::N::T)i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (warning) Redundant code: Found unused cast of expression 'i'.\n", errout.str());
check("void f(int (g)(int a, int b)) {\n" // #10873
" int p = 0, q = 1;\n"
" (g)(p, q);\n"
"}\n"
"void f() {\n"
" char buf[10];\n"
" (sprintf)(buf, \"%d\", 42);\n"
" (printf)(\"abc\");\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct S; struct T; struct U;\n"
"void f() {\n"
" T t;\n"