diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 2580e550f..16b02f1ea 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1042,6 +1042,11 @@ bool isLikelyStreamRead(bool cpp, const Token *op) return (!parent->astOperand1()->valueType() || !parent->astOperand1()->valueType()->isIntegral()); } +bool isCPPCast(const Token* tok) +{ + return tok && Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1() && tok->astOperand1()->str().find("_cast") != std::string::npos; +} + bool isConstVarExpression(const Token *tok) { if (!tok) @@ -1052,7 +1057,7 @@ bool isConstVarExpression(const Token *tok) std::vector args = getArguments(tok); return std::all_of(args.begin(), args.end(), &isConstVarExpression); } - if (Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1()->str().find("_cast") != std::string::npos) { + if (isCPPCast(tok)) { return isConstVarExpression(tok->astOperand2()); } if (Token::Match(tok, "( %type%")) diff --git a/lib/astutils.h b/lib/astutils.h index 1725de938..28e9e7346 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -160,6 +160,8 @@ const Token *findLambdaEndToken(const Token *first); */ bool isLikelyStreamRead(bool cpp, const Token *op); +bool isCPPCast(const Token* tok); + bool isConstVarExpression(const Token *tok); /** diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f469c13cd..0c79a73d0 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2798,12 +2798,15 @@ void CheckOther::checkConstArgument() continue; if (!tok->hasKnownIntValue()) continue; - if (Token::Match(tok, "%var%")) - continue; if (Token::Match(tok, "++|--")) continue; if (isConstVarExpression(tok)) continue; + const Token * tok2 = tok; + if (isCPPCast(tok2)) + tok2 = tok2->astOperand2(); + if (Token::Match(tok2, "%var%")) + continue; constArgumentError(tok, tok->astParent()->previous(), &tok->values().front()); } } diff --git a/test/testother.cpp b/test/testother.cpp index 6dd5442e9..09298a645 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7554,6 +7554,13 @@ private: " g(x + 1);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void g(int);\n" + "void f() {\n" + " char i = 1;\n" + " g(static_cast(i));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } };