Fix FN variableScope (#5273)

Co-authored-by: chrchr-github <chrchr@github>
This commit is contained in:
chrchr-github 2023-08-01 23:56:24 +02:00 committed by GitHub
parent 4452ae6d09
commit 931a59a724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -1074,11 +1074,13 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
if (var->isArrayOrPointer()) {
int argn{};
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { // var passed to function?
if (ftok->function() && Function::returnsPointer(ftok->function()))
return false;
const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned
if (!ret.empty() && ret.back() == '*')
return false;
if (ftok->next()->astParent()) { // return value used?
if (ftok->function() && Function::returnsPointer(ftok->function()))
return false;
const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned
if (!ret.empty() && ret.back() == '*')
return false;
}
}
}
}

View File

@ -1656,6 +1656,16 @@ private:
" printf(\"%d: %s\\n\", err, msg);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("char* g(char* dst, const char* src);\n"
"void f(int err, const char* src) {\n"
" const char* msg = \"Success\";\n"
" char buf[42];\n"
" if (err != 0)\n"
" g(buf, src);\n"
" printf(\"%d: %s\\n\", err, msg);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'buf' can be reduced.\n", errout.str());
}
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)