diff --git a/cfg/std.cfg b/cfg/std.cfg index 762c76b30..9760afb7c 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -8555,6 +8555,13 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + + + + false + + + malloc calloc diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d8c9298af..3afef417d 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2472,8 +2472,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& ; else if (var->smartPointerType() && var->smartPointerType()->classScope && isConstMemberFunc(var->smartPointerType()->classScope, end)) { ; - } - else if (hasOverloadedMemberAccess(end, var->typeScope())) { + } else if (var->isSmartPointer() && Token::simpleMatch(tok1->next(), ".") && tok1->next()->originalName().empty() && mSettings->library.isFunctionConst(end)) { + ; + } else if (hasOverloadedMemberAccess(end, var->typeScope())) { ; } else if (!var->typeScope() || !isConstMemberFunc(var->typeScope(), end)) return false; diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index fd00d8fc7..7b8e6b02b 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -4703,4 +4704,13 @@ void beginEnd() std::cend(arr); //cppcheck-suppress ignoredReturnValue std::crend(arr); +} + +void smartPtr_get() +{ + std::unique_ptr p; + //cppcheck-suppress ignoredReturnValue + p.get(); + //cppcheck-suppress nullPointer + *p = 1; } \ No newline at end of file diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index e343b599f..427d07474 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2981,7 +2981,7 @@ private: " return ptr.get();\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:4] -> [test.cpp:3] -> [test.cpp:4]: (error) Returning object that points to local variable 'ptr' that will be invalid when returning.\n", + "[test.cpp:4] -> [test.cpp:3] -> [test.cpp:4]: (error) Returning pointer to local variable 'ptr' that will be invalid when returning.\n", errout.str()); } void danglingLifetime() { diff --git a/test/testclass.cpp b/test/testclass.cpp index a9015dc84..bbef69603 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -36,31 +36,10 @@ public: TestClass() : TestFixture("TestClass") {} private: - Settings settings0 = settingsBuilder().severity(Severity::style).build(); - Settings settings1 = settingsBuilder().severity(Severity::warning).build(); + Settings settings0 = settingsBuilder().severity(Severity::style).library("std.cfg").build(); + Settings settings1 = settingsBuilder().severity(Severity::warning).library("std.cfg").build(); void run() override { - // Load std.cfg configuration - { - const char xmldata[] = "\n" - "\n" - " \n" - " malloc\n" - " free\n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - ""; - ASSERT(settings0.library.loadxmldata(xmldata, sizeof(xmldata))); - ASSERT(settings1.library.loadxmldata(xmldata, sizeof(xmldata))); - } - - TEST_CASE(virtualDestructor1); // Base class not found => no error TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor TEST_CASE(virtualDestructor3); // Base class has a destructor, but it's not virtual @@ -6529,6 +6508,13 @@ private: "[test.cpp:3]: (style, inconclusive) Technically the member function 'S::g' can be const.\n" "[test.cpp:4]: (style, inconclusive) Technically the member function 'S::h' can be const.\n", errout.str()); + + checkConst("struct S {\n" + " bool f() { return p.get() != nullptr; }\n" + " std::shared_ptr p;\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n", + errout.str()); } void const89() { // #11654