Skip to content

Commit e70a888

Browse files
Fix constPointer FP #11674, TODOs (#4976)
* Fix constPointer TODOs * Fix #11674 FP constParameterPointer when function signature is fixed * Format
1 parent 71f28fc commit e70a888

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

lib/checkother.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,9 @@ void CheckOther::checkConstPointer()
15451545
const ValueType* const vt = tok->valueType();
15461546
if (!vt)
15471547
continue;
1548-
if ((vt->pointer != 1 && !(vt->pointer == 2 && var->isArray())) || (vt->constness & 1) || vt->reference != Reference::None)
1548+
if ((vt->pointer != 1 && !(vt->pointer == 2 && var->isArray())) || (vt->constness & 1))
1549+
continue;
1550+
if (var->typeStartToken()->isTemplateArg())
15491551
continue;
15501552
if (std::find(nonConstPointers.cbegin(), nonConstPointers.cend(), var) != nonConstPointers.cend())
15511553
continue;
@@ -1618,7 +1620,7 @@ void CheckOther::checkConstPointer()
16181620
continue;
16191621
if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
16201622
continue;
1621-
constVariableError(p, nullptr);
1623+
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
16221624
}
16231625
}
16241626
}

test/testother.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,15 +3298,15 @@ class TestOther : public TestFixture {
32983298
" for (const auto& h : v)\n"
32993299
" if (h) {}\n"
33003300
"}\n");
3301-
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", "", errout.str());
3301+
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", errout.str());
33023302

33033303
check("void f(const std::vector<int*>& v) {\n"
33043304
" for (const auto& p : v)\n"
33053305
" if (p == nullptr) {}\n"
33063306
" for (const auto* p : v)\n"
33073307
" if (p == nullptr) {}\n"
33083308
"}\n");
3309-
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", "", errout.str());
3309+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", errout.str());
33103310

33113311
check("void f(std::vector<int*>& v) {\n"
33123312
" for (const auto& p : v)\n"
@@ -3318,10 +3318,9 @@ class TestOther : public TestFixture {
33183318
" for (const int* p : v)\n"
33193319
" if (p == nullptr) {}\n"
33203320
"}\n");
3321-
TODO_ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
3322-
"[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n",
3323-
"[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n",
3324-
errout.str());
3321+
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
3322+
"[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n",
3323+
errout.str());
33253324

33263325
check("void f(std::vector<const int*>& v) {\n"
33273326
" for (const auto& p : v)\n"
@@ -3511,6 +3510,18 @@ class TestOther : public TestFixture {
35113510
" g(c);\n"
35123511
"}\n");
35133512
ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 'c' can be declared as pointer to const\n", errout.str());
3513+
3514+
check("typedef void (*cb_t)(int*);\n" // #11674
3515+
"void cb(int* p) {\n"
3516+
" if (*p) {}\n"
3517+
"}\n"
3518+
"void g(cb_t);\n"
3519+
"void f() {\n"
3520+
" g(cb);\n"
3521+
"}\n");
3522+
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (style) Parameter 'p' can be declared as pointer to const. "
3523+
"However it seems that 'cb' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s).\n",
3524+
errout.str());
35143525
}
35153526

35163527
void switchRedundantAssignmentTest() {

0 commit comments

Comments
 (0)