From 0ba883f64bbb52299a40c5f8844dbce8d373e07c Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 27 Nov 2026 19:34:33 +0100 Subject: [PATCH 1/5] Fix #15226 --- lib/astutils.cpp | 2 +- lib/checkother.cpp | 2 +- test/testother.cpp | 12 +++++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e2e19b6bd91..ff677bd225a 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2369,7 +2369,7 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn) parent = parent->astParent(); while (parent && parent->isCast()) parent = parent->astParent(); - if (Token::Match(parent, "[+-]") && parent->valueType() && parent->valueType()->pointer) + if (Token::Match(parent, "[+-]") && (!parent->valueType() || parent->valueType()->pointer)) parent = parent->astParent(); // passing variable to subfunction? diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 79697a2e6d7..c931b1e30d4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2063,7 +2063,7 @@ void CheckOtherImpl::checkConstPointer() int argn = -1; if (Token::simpleMatch(gparent, "return")) { const Function* function = getEnclosingFunction(var); - if (function && (!Function::returnsReference(function) || Function::returnsConst(function))) + if (function && (!Function::returnsReference(function, /*unknown*/ true) || Function::returnsConst(function))) continue; } else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) { diff --git a/test/testother.cpp b/test/testother.cpp index d6025e9a640..7b3850e452d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4966,7 +4966,9 @@ class TestOther : public TestFixture { " int i = 0;\n" " return [](int* p) { return *p; }(&i);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); + TODO_ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", + "", + errout_str()); check("struct S {\n" // #14571 " char* c;\n" @@ -5087,6 +5089,14 @@ class TestOther : public TestFixture { "void g(__attribute__((unused)) int *p) {}\n" "void h(int *p) {}\n"); ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); + + check("void f(int* p, U n) {\n" // #15226 + " g(*(p + n));\n" + "}\n" + "U g(int* p, int n) {\n" + " return *(p + n);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void constArray() { From 6467482b42cf7ffd9c0367ab3318329de5bb2f61 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:45:02 +0100 Subject: [PATCH 2/5] Update symboldatabase.cpp [skip ci] --- lib/symboldatabase.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index d321281b31d..53c3be0a64c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3352,15 +3352,18 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl if (function->type != FunctionType::eFunction && function->type != FunctionType::eOperatorEqual && function->type != FunctionType::eLambda) return false; const Token* defStart = function->retDef; - if (!defStart) + if (!defStart && !function->isLambda()) return unknown; const Token* defEnd = function->returnDefEnd(); if (!defEnd) return unknown; if (defEnd == defStart) return unknown; - if (pred(defStart, defEnd)) + bool conclusive = false; + if (pred(defStart, defEnd, conclusive)) return true; + else if (conclusive) + return false; if (Token::Match(defEnd->tokAt(-1), "*|&|&&")) return false; // void STDCALL foo() @@ -3383,7 +3386,7 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl } } assert(defEnd != defStart); - if (pred(defStart, defEnd)) + if (pred(defStart, defEnd, conclusive)) return true; if (isUnknownType(defStart, defEnd)) { const Token* tok = function->token ? function->token->next() : function->tokenDef->next(); @@ -3396,35 +3399,39 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl bool Function::returnsConst(const Function* function, bool unknown) { - return checkReturns(function, unknown, false, [](const Token* defStart, const Token* defEnd) { + return checkReturns(function, unknown, false, [](const Token* defStart, const Token* defEnd, bool& /*conclusive*/) { return Token::findsimplematch(defStart, "const", defEnd); }); } bool Function::returnsReference(const Function* function, bool unknown, bool includeRValueRef) { - return checkReturns(function, unknown, false, [includeRValueRef](const Token* /*defStart*/, const Token* defEnd) { + return checkReturns(function, unknown, false, [includeRValueRef](const Token* /*defStart*/, const Token* defEnd, bool& conclusive) { + if (Token::simpleMatch(defEnd->previous(), "auto") || Token::simpleMatch(defEnd, "[")) { + conclusive = true; + return false; + } return includeRValueRef ? Token::Match(defEnd->previous(), "&|&&") : Token::simpleMatch(defEnd->previous(), "&"); }); } bool Function::returnsPointer(const Function* function, bool unknown) { - return checkReturns(function, unknown, false, [](const Token* /*defStart*/, const Token* defEnd) { + return checkReturns(function, unknown, false, [](const Token* /*defStart*/, const Token* defEnd, bool& /*conclusive*/) { return Token::simpleMatch(defEnd->previous(), "*"); }); } bool Function::returnsStandardType(const Function* function, bool unknown) { - return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd) { + return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd, bool& /*conclusive*/) { return defEnd->previous() && defEnd->previous()->isStandardType(); }); } bool Function::returnsVoid(const Function* function, bool unknown) { - return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd) { + return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd, bool& /*conclusive*/) { return Token::simpleMatch(defEnd->previous(), "void"); }); } From f2333b8091b9dddacc8b3b657c80e1559dd0e229 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:46:25 +0100 Subject: [PATCH 3/5] Update testother.cpp --- test/testother.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 5a26c222532..abc3a44e987 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4966,9 +4966,8 @@ class TestOther : public TestFixture { " int i = 0;\n" " return [](int* p) { return *p; }(&i);\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", - "", - errout_str()); + ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", + errout_str()); check("struct S {\n" // #14571 " char* c;\n" @@ -5104,8 +5103,11 @@ class TestOther : public TestFixture { "}\n" "U g(int* p, int n) {\n" " return *(p + n);\n" + "}\n" + "auto h(int* p) {\n" + " return *p;\n" "}\n"); - ASSERT_EQUALS("", errout_str()); + ASSERT_EQUALS("[test.cpp:7:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); } void constArray() { From 13b50f402ea4922d84dba2d54a7fcc90f6fce0a7 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 30 Nov 2026 17:49:15 +0100 Subject: [PATCH 4/5] Fix --- lib/symboldatabase.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 53c3be0a64c..404578cf621 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3362,8 +3362,10 @@ static bool checkReturns(const Function* function, bool unknown, bool emptyEnabl bool conclusive = false; if (pred(defStart, defEnd, conclusive)) return true; - else if (conclusive) + if (conclusive) return false; + if (!defStart) + return unknown; if (Token::Match(defEnd->tokAt(-1), "*|&|&&")) return false; // void STDCALL foo() From 4f770a2794ec74506ab564faf79c82aaec021e24 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 30 Nov 2026 19:33:19 +0100 Subject: [PATCH 5/5] Fix --- lib/astutils.cpp | 2 +- test/testother.cpp | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a077beac725..288631b5917 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2369,7 +2369,7 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn) parent = parent->astParent(); while (parent && parent->isCast()) parent = parent->astParent(); - if (Token::Match(parent, "[+-]") && (!parent->valueType() || parent->valueType()->pointer)) + if (Token::Match(parent, "[+-]") && (astIsPointer(parent) || astIsPointer(tok))) parent = parent->astParent(); // passing variable to subfunction? diff --git a/test/testother.cpp b/test/testother.cpp index abc3a44e987..ee27047cb03 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5104,10 +5104,20 @@ class TestOther : public TestFixture { "U g(int* p, int n) {\n" " return *(p + n);\n" "}\n" - "auto h(int* p) {\n" + "auto h1(int* p) {\n" " return *p;\n" + "}\n" + "void h2(int* p) {\n" + " g(h() + *p);\n" + "}\n" + "void h3(int* p) {\n" + " int* q = p;\n" + " g(h() - *q);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:7:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:7:14]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" + "[test.cpp:10:14]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" + "[test.cpp:14:10]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); } void constArray() {