Skip to content

Commit

Permalink
Fix FN variableScope (danmar#5273)
Browse files Browse the repository at this point in the history
Co-authored-by: chrchr-github <chrchr@github>
  • Loading branch information
chrchr-github and chrchr-github authored Aug 1, 2023
1 parent 4452ae6 commit 931a59a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,16 @@ class TestOther : public TestFixture {
" 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__)
Expand Down

0 comments on commit 931a59a

Please sign in to comment.