diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d9278a290..37f23d417 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1545,7 +1545,9 @@ void CheckOther::checkConstPointer() const ValueType* const vt = tok->valueType(); if (!vt) continue; - if ((vt->pointer != 1 && !(vt->pointer == 2 && var->isArray())) || (vt->constness & 1) || vt->reference != Reference::None) + if ((vt->pointer != 1 && !(vt->pointer == 2 && var->isArray())) || (vt->constness & 1)) + continue; + if (var->typeStartToken()->isTemplateArg()) continue; if (std::find(nonConstPointers.cbegin(), nonConstPointers.cend(), var) != nonConstPointers.cend()) continue; @@ -1618,7 +1620,7 @@ void CheckOther::checkConstPointer() continue; if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) continue; - constVariableError(p, nullptr); + constVariableError(p, p->isArgument() ? p->scope()->function : nullptr); } } } diff --git a/test/testother.cpp b/test/testother.cpp index c950c076e..f4278a09c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3298,7 +3298,7 @@ private: " for (const auto& h : v)\n" " if (h) {}\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", errout.str()); check("void f(const std::vector& v) {\n" " for (const auto& p : v)\n" @@ -3306,7 +3306,7 @@ private: " for (const auto* p : v)\n" " if (p == nullptr) {}\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); check("void f(std::vector& v) {\n" " for (const auto& p : v)\n" @@ -3318,10 +3318,9 @@ private: " for (const int* p : v)\n" " if (p == nullptr) {}\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n" - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", - "[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n", - errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n" + "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", + errout.str()); check("void f(std::vector& v) {\n" " for (const auto& p : v)\n" @@ -3511,6 +3510,18 @@ private: " g(c);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 'c' can be declared as pointer to const\n", errout.str()); + + check("typedef void (*cb_t)(int*);\n" // #11674 + "void cb(int* p) {\n" + " if (*p) {}\n" + "}\n" + "void g(cb_t);\n" + "void f() {\n" + " g(cb);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (style) Parameter 'p' can be declared as pointer to const. " + "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", + errout.str()); } void switchRedundantAssignmentTest() {