Skip to content

Commit 931a59a

Browse files
chrchr-githubchrchr-github
andauthored
Fix FN variableScope (#5273)
Co-authored-by: chrchr-github <chrchr@github>
1 parent 4452ae6 commit 931a59a

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

lib/checkother.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,13 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
10741074
if (var->isArrayOrPointer()) {
10751075
int argn{};
10761076
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { // var passed to function?
1077-
if (ftok->function() && Function::returnsPointer(ftok->function()))
1078-
return false;
1079-
const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned
1080-
if (!ret.empty() && ret.back() == '*')
1081-
return false;
1077+
if (ftok->next()->astParent()) { // return value used?
1078+
if (ftok->function() && Function::returnsPointer(ftok->function()))
1079+
return false;
1080+
const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned
1081+
if (!ret.empty() && ret.back() == '*')
1082+
return false;
1083+
}
10821084
}
10831085
}
10841086
}

test/testother.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,16 @@ class TestOther : public TestFixture {
16561656
" printf(\"%d: %s\\n\", err, msg);\n"
16571657
"}\n");
16581658
ASSERT_EQUALS("", errout.str());
1659+
1660+
check("char* g(char* dst, const char* src);\n"
1661+
"void f(int err, const char* src) {\n"
1662+
" const char* msg = \"Success\";\n"
1663+
" char buf[42];\n"
1664+
" if (err != 0)\n"
1665+
" g(buf, src);\n"
1666+
" printf(\"%d: %s\\n\", err, msg);\n"
1667+
"}\n");
1668+
ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'buf' can be reduced.\n", errout.str());
16591669
}
16601670

16611671
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)

0 commit comments

Comments
 (0)