From 49147f95fe3ea03b617c82cb24ec405c317ddbfd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 28 Mar 2022 22:15:40 +0200 Subject: [PATCH] Fix #10918 FP constStatement with dynamic_cast (#3954) --- lib/checkother.cpp | 5 ++++- test/testincompletestatement.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 812f006dc..58675bbad 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1777,8 +1777,11 @@ static bool isConstStatement(const Token *tok, bool cpp) return true; if (Token::simpleMatch(tok->previous(), "sizeof (")) return true; - if (isCPPCast(tok)) + if (isCPPCast(tok)) { + if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->next()->link()->previous(), "& >")) + return false; return isWithoutSideEffects(cpp, tok) && isConstStatement(tok->astOperand2(), cpp); + } else if (tok->isCast() && tok->next() && tok->next()->isStandardType()) return isWithoutSideEffects(cpp, tok->astOperand1()) && isConstStatement(tok->astOperand1(), cpp); if (Token::simpleMatch(tok, ".")) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 0e7bdb6bc..3bb3526da 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -283,6 +283,17 @@ private: " ((struct foo *)(0x1234))->xy = 1;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("bool f(const std::exception& e) {\n" // #10918 + " try {\n" + " dynamic_cast(e);\n" + " return true;\n" + " }\n" + " catch (...) {\n" + " return false;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void increment() {