Skip to content

Commit

Permalink
Partial fix for #11927 FP knownArgument with unknown function type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Sep 8, 2023
1 parent e4f92f6 commit 4d5e84a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11067,6 +11067,21 @@ class TestOther : public TestFixture {
" }\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() {
Expand Down

0 comments on commit 4d5e84a

Please sign in to comment.