From 6a8bd981b53651bbe74ffa98e964a6632781c8ea Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 28 Feb 2022 18:28:23 +0100 Subject: [PATCH] Fix #10825 CheckClass::isMemberVar found used member variable with varid 0 (#3863) * Fix #10825 CheckClass::isMemberVar found used member variable with varid 0 * Restore call to simpleMatch() --- lib/checkclass.cpp | 3 +++ test/testclass.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d760c0d27..be7795af3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2037,6 +2037,9 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const return true; } else if (Token::simpleMatch(tok->tokAt(-3), "( * this )")) { return true; + } else if (Token::Match(tok->tokAt(-3), "%name% ) . %name%")) { + tok = tok->tokAt(-3); + again = true; } else if (Token::Match(tok->tokAt(-2), "%name% . %name%")) { tok = tok->tokAt(-2); again = true; diff --git a/test/testclass.cpp b/test/testclass.cpp index b5b82efda..5adff2c96 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -192,6 +192,7 @@ private: TEST_CASE(const73); // ticket #10735 TEST_CASE(const74); // ticket #10671 TEST_CASE(const75); // ticket #10065 + TEST_CASE(const76); // ticket #10825 TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(assigningPointerToPointerIsNotAConstOperation); @@ -5994,6 +5995,20 @@ private: ASSERT_EQUALS("[test.cpp:4]: (style, inconclusive) Technically the member function 'N::S::f' can be const.\n", errout.str()); } + void const76() { // #10825 + checkConst("struct S {\n" + " enum E {};\n" + " void f(const T* t);\n" + " E e;\n" + "};\n" + "struct T { void e(); };\n" + "void S::f(const T* t) {\n" + " const_cast(t)->e();\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (performance, inconclusive) Technically the member function 'S::f' can be static (but you may consider moving to unnamed namespace).\n", + errout.str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"