From 97ce56985941393fb88c1c1b2ba2526ffddcc8e7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 17 Mar 2022 12:47:27 +0100 Subject: [PATCH] Fix #10873 FP constStatement with extra parentheses in function call (#3911) * Fix #10873 FP constStatement with extra parentheses in function call * Format --- lib/checkother.cpp | 4 +--- test/testincompletestatement.cpp | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 0aa218a23..a671d0b58 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 950268f93..336f2a38a 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -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"