From a49d277e0da99f6585c0b94baee1c51afad69496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 29 Jun 2020 13:09:01 +0200 Subject: [PATCH] Fixed #6471 (FP functionConst - member function modifying member variable after cast (inconclusive)) --- lib/checkclass.cpp | 3 +++ test/testclass.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 049c020bf..7300b8e29 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1991,6 +1991,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& if (tok1->str() == "this" && tok1->previous()->isAssignmentOp()) return false; + // non const pointer cast + if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() && !Token::simpleMatch(tok1->astParent(), "( const")) + return false; const Token* lhs = tok1->previous(); if (lhs->str() == "&") { diff --git a/test/testclass.cpp b/test/testclass.cpp index 82d559000..a5db98b02 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -170,6 +170,7 @@ private: TEST_CASE(const65); // ticket #8693 TEST_CASE(const66); // ticket #7714 TEST_CASE(const67); // ticket #9193 + TEST_CASE(const68); // ticket #6471 TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(assigningPointerToPointerIsNotAConstOperation); @@ -5514,6 +5515,17 @@ private: ASSERT_EQUALS("[test.cpp:8]: (style, inconclusive) Technically the member function 'Test::get' can be const.\n", errout.str()); } + void const68() { // #6471 + checkConst("class MyClass {\n" + " void clear() {\n" + " SVecPtr v = (SVecPtr) m_data;\n" + " v->clear();\n" + " }\n" + " void* m_data;\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"