From e9e1bceb53b440c1eb2bda60c7162eb9046ddda1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 14 Oct 2024 10:23:06 +0200 Subject: [PATCH 1/3] fix #13203 --- lib/checkclass.cpp | 10 +++++++-- test/testclass.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 2fec3552af7..fd97425c1ef 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -856,7 +856,9 @@ void CheckClass::initializeVarList(const Function &func, std::listnext(), "::| %name%") && !Token::Match(ftok->next(), "*| this . %name%") && !Token::Match(ftok->next(), "* %name% =") && - !Token::Match(ftok->next(), "( * this ) . %name%")) + !Token::Match(ftok->next(), "( * this ) . %name%") && + !Token::Match(ftok->next(), "( * %name% ) =") && + !Token::Match(ftok->next(), "* ( %name% ) =")) continue; // Goto the first token in this statement.. @@ -1060,7 +1062,11 @@ void CheckClass::initializeVarList(const Function &func, std::list + void checkConstructors_(const char (&code)[size], const char* file, int line) { + const Settings settings = settingsBuilder().severity(Severity::warning).build(); + + // Tokenize.. + SimpleTokenizer tokenizer(settings, *this); + ASSERT_LOC(tokenizer.tokenize(code), file, line); + + // Check.. + CheckClass checkClass(&tokenizer, &settings, this); + (checkClass.constructors)(); + } + + void operatorEqPtrAssign() { // ticket #13203 + checkConstructors("struct S {\n" + " int* m_data;\n" + " S() : m_data(new int()) {}\n" + " S& operator=(const S& s) {\n" + " if (&s != this) {\n" + " *(m_data) = *(s.m_data);\n" + " }\n" + " return *this;\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); + + checkConstructors("struct S {\n" + " int* m_data;\n" + " S() : m_data(new int()) {}\n" + " S& operator=(const S& s) {\n" + " if (&s != this) {\n" + " (*m_data) = *(s.m_data);\n" + " }\n" + " return *this;\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); + + checkConstructors("struct S {\n" + " int* m_data;\n" + " S() : m_data(new int()) {}\n" + " S& operator=(const S& s) {\n" + " return *this;\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'S::m_data' is not assigned a value in 'S::operator='.\n", errout_str()); + } + // Check that base classes have virtual destructors #define checkVirtualDestructor(...) checkVirtualDestructor_(__FILE__, __LINE__, __VA_ARGS__) template From db91132095772969e04afacb5e842244908482a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 14 Oct 2024 10:26:26 +0200 Subject: [PATCH 2/3] remove unnecessary pipes in match pattern --- lib/checkclass.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index fd97425c1ef..11740c20e4a 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1062,9 +1062,9 @@ void CheckClass::initializeVarList(const Function &func, std::list