Skip to content

Commit 4d5e84a

Browse files
Partial fix for #11927 FP knownArgument with unknown function type (#5413)
1 parent e4f92f6 commit 4d5e84a

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3804,8 +3804,14 @@ void CheckOther::knownArgumentError(const Token *tok, const Token *ftok, const V
38043804
const std::string &expr = tok->expressionString();
38053805
const std::string &fun = ftok->str();
38063806

3807+
std::string ftype = "function ";
3808+
if (ftok->type())
3809+
ftype = "constructor ";
3810+
else if (fun == "{")
3811+
ftype = "init list ";
3812+
38073813
const char *id;
3808-
std::string errmsg = "Argument '" + expr + "' to function " + fun + " is always " + std::to_string(intvalue) + ". ";
3814+
std::string errmsg = "Argument '" + expr + "' to " + ftype + fun + " is always " + std::to_string(intvalue) + ". ";
38093815
if (!isVariableExpressionHidden) {
38103816
id = "knownArgument";
38113817
errmsg += "It does not matter what value '" + varexpr + "' has.";

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ static void setTokenValue(Token* tok,
635635
// Ensure that the comma isn't a function call
636636
if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") &&
637637
(!Token::Match(callParent, "( %name%") || settings->library.isNotLibraryFunction(callParent->next())) &&
638-
!(callParent->str() == "(" && Token::simpleMatch(callParent->astOperand1(), "*")))) {
638+
!(callParent->str() == "(" && (Token::simpleMatch(callParent->astOperand1(), "*") || Token::Match(callParent->astOperand1(), "%name%"))))) {
639639
setTokenValue(parent, std::move(value), settings);
640640
return;
641641
}

test/testother.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11067,6 +11067,21 @@ class TestOther : public TestFixture {
1106711067
" }\n"
1106811068
"}\n");
1106911069
ASSERT_EQUALS("", errout.str());
11070+
11071+
// #11927
11072+
check("void f(func_t func, int i) {\n"
11073+
" (func)(i, 0);\n"
11074+
"}\n");
11075+
ASSERT_EQUALS("", errout.str());
11076+
11077+
check("struct S { int i; };\n"
11078+
"void f(int i) {\n"
11079+
" const int a[] = { i - 1 * i, 0 };\n"
11080+
" auto s = S{ i - 1 * i };\n"
11081+
"}\n");
11082+
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"
11083+
"[test.cpp:4]: (style) Argument 'i-1*i' to constructor S is always 0. It does not matter what value 'i' has.\n",
11084+
errout.str());
1107011085
}
1107111086

1107211087
void knownArgumentHiddenVariableExpression() {

0 commit comments

Comments
 (0)