Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,17 @@ static bool isCastToInteger(const Token* tok)
return tok && tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && tok->valueType()->pointer == 0;
}

static const Function* getEnclosingFunction(const Variable* var)
{
if (var->isArgument())
return var->scope()->function;
const Scope* scope = var->scope();
while (scope && scope->type != ScopeType::eFunction) {
scope = scope->nestedIn;
}
return scope ? scope->function : nullptr;
}

void CheckOther::checkConstPointer()
{
if (!mSettings->severity.isEnabled(Severity::style) &&
Expand Down Expand Up @@ -1917,7 +1928,7 @@ void CheckOther::checkConstPointer()
continue;
int argn = -1;
if (Token::simpleMatch(gparent, "return")) {
const Function* function = gparent->scope()->function;
const Function* function = getEnclosingFunction(var);
if (function && (!Function::returnsReference(function) || Function::returnsConst(function)))
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13214,14 +13214,14 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct A { bool x; };\n"
check("struct A { bool x; };\n" // #14481
"bool f(A* a) {\n"
" if (a) {\n"
" return a->x;\n"
" }\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n", errout_str());

check("struct A { int* x; };\n"
"bool f(A a) {\n"
Expand Down
Loading