From 4d5e84aa5f4ca73f963d833c3f4e6c92b47ba330 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 8 Sep 2023 16:41:07 +0200 Subject: [PATCH] Partial fix for #11927 FP knownArgument with unknown function type (#5413) --- lib/checkother.cpp | 8 +++++++- lib/valueflow.cpp | 2 +- test/testother.cpp | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 13ae85df4..fef42ec09 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3804,8 +3804,14 @@ void CheckOther::knownArgumentError(const Token *tok, const Token *ftok, const V const std::string &expr = tok->expressionString(); const std::string &fun = ftok->str(); + std::string ftype = "function "; + if (ftok->type()) + ftype = "constructor "; + else if (fun == "{") + ftype = "init list "; + const char *id; - std::string errmsg = "Argument '" + expr + "' to function " + fun + " is always " + std::to_string(intvalue) + ". "; + std::string errmsg = "Argument '" + expr + "' to " + ftype + fun + " is always " + std::to_string(intvalue) + ". "; if (!isVariableExpressionHidden) { id = "knownArgument"; errmsg += "It does not matter what value '" + varexpr + "' has."; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 396879b90..3036bdc42 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -635,7 +635,7 @@ static void setTokenValue(Token* tok, // Ensure that the comma isn't a function call if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") && (!Token::Match(callParent, "( %name%") || settings->library.isNotLibraryFunction(callParent->next())) && - !(callParent->str() == "(" && Token::simpleMatch(callParent->astOperand1(), "*")))) { + !(callParent->str() == "(" && (Token::simpleMatch(callParent->astOperand1(), "*") || Token::Match(callParent->astOperand1(), "%name%"))))) { setTokenValue(parent, std::move(value), settings); return; } diff --git a/test/testother.cpp b/test/testother.cpp index 829265a37..6ad27e8e5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -11067,6 +11067,21 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #11927 + check("void f(func_t func, int i) {\n" + " (func)(i, 0);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("struct S { int i; };\n" + "void f(int i) {\n" + " const int a[] = { i - 1 * i, 0 };\n" + " auto s = S{ i - 1 * i };\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Argument 'i-1*i' to init list { is always 0. It does not matter what value 'i' has.\n" + "[test.cpp:4]: (style) Argument 'i-1*i' to constructor S is always 0. It does not matter what value 'i' has.\n", + errout.str()); } void knownArgumentHiddenVariableExpression() {