Partial fix for #11927 FP knownArgument with unknown function type (#5413)

This commit is contained in:
chrchr-github 2023-09-08 16:41:07 +02:00 committed by GitHub
parent e4f92f6979
commit 4d5e84aa5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

@ -3804,8 +3804,14 @@ void CheckOther::knownArgumentError(const Token *tok, const Token *ftok, const V
const std::string &expr = tok->expressionString(); const std::string &expr = tok->expressionString();
const std::string &fun = ftok->str(); const std::string &fun = ftok->str();
std::string ftype = "function ";
if (ftok->type())
ftype = "constructor ";
else if (fun == "{")
ftype = "init list ";
const char *id; 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) { if (!isVariableExpressionHidden) {
id = "knownArgument"; id = "knownArgument";
errmsg += "It does not matter what value '" + varexpr + "' has."; errmsg += "It does not matter what value '" + varexpr + "' has.";

View File

@ -635,7 +635,7 @@ static void setTokenValue(Token* tok,
// Ensure that the comma isn't a function call // Ensure that the comma isn't a function call
if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") && if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") &&
(!Token::Match(callParent, "( %name%") || settings->library.isNotLibraryFunction(callParent->next())) && (!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); setTokenValue(parent, std::move(value), settings);
return; return;
} }

View File

@ -11067,6 +11067,21 @@ private:
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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() { void knownArgumentHiddenVariableExpression() {