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() {