Skip to content

Commit 1a9bdeb

Browse files
Fix #14510 FP constParameterPointer (lambda decaying to function pointer) (danmar#8234)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 6c20dbe commit 1a9bdeb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/checkother.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,8 @@ void CheckOther::checkConstPointer()
18691869
continue;
18701870
if (!var->isLocal() && !var->isArgument())
18711871
continue;
1872+
if (var->isArgument() && var->scope() && var->scope()->type == ScopeType::eLambda)
1873+
continue;
18721874
const Token* const nameTok = var->nameToken();
18731875
if (tok == nameTok && var->isLocal() && !astIsRangeBasedForDecl(nameTok)) {
18741876
if (var->isReference() && var->isPointer()) {

test/testother.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4676,6 +4676,27 @@ class TestOther : public TestFixture {
46764676
ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
46774677
"[test.cpp:4:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
46784678
errout_str());
4679+
4680+
check("using fp_t = int (*)(int*);\n" // #14510
4681+
"fp_t g_fp;\n"
4682+
"struct S { fp_t m_fp; };\n"
4683+
"void g(fp_t);\n"
4684+
"S f(S* s) {\n"
4685+
" g_fp = [](int* p) { return *p; };\n"
4686+
" s->m_fp = [](int* p) { return *p; };\n"
4687+
" g([](int* p) { return *p; });\n"
4688+
" auto x = [](int* p) { return *p; };\n"
4689+
" g(x);\n"
4690+
" return { [](int* p) { return *p; } };\n"
4691+
"}\n");
4692+
ASSERT_EQUALS("", errout_str());
4693+
4694+
check("void f() {\n"
4695+
" int i = 0;\n"
4696+
" auto x = [&]() { int* p = &i; if (*p) {} };\n"
4697+
" x();\n"
4698+
"}\n");
4699+
ASSERT_EQUALS("[test.cpp:3:27]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str());
46794700
}
46804701

46814702
void constArray() {

0 commit comments

Comments
 (0)