From 7d216adb423433eee3b19771fa819611457dd115 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:34:11 +0100 Subject: [PATCH 1/3] Update testother.cpp --- test/testother.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 4dfb00ac03b..5a8d3f1017e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -10493,6 +10493,23 @@ class TestOther : public TestFixture { " return j;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("struct S {\n" // #12894 + " std::string a;\n" + " void f(const S& s);\n" + " void g(const S& s);\n" + "};\n" + "void S::f(const S& s) {\n" + " std::string x = a;\n" + " this->operator=(s);\n" + " a = x;\n" + "}\n" + "void S::g(const S& s) {\n" + " std::string x = a;\n" + " operator=(s);\n" + " a = x;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void varFuncNullUB() { // #4482 From 3ae85b9d59d08c308ea262d0a212b757dde72175 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:35:27 +0100 Subject: [PATCH 2/3] Update astutils.cpp --- lib/astutils.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a762d500505..ced367c1e42 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1116,19 +1116,23 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth) ++depth; // calling nonstatic method? - if (Token::Match(expr, "%name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) { - // is it a method of this? - const Scope* fScope = expr->scope(); - while (!fScope->functionOf && fScope->nestedIn) - fScope = fScope->nestedIn; - - const Scope* classScope = fScope->functionOf; - if (classScope && classScope->function) - classScope = classScope->function->token->scope(); - - if (classScope && classScope->isClassOrStruct()) - return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn); - return false; + if (Token::Match(expr, "%name% (")) { + if (expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) { + // is it a method of this? + const Scope* fScope = expr->scope(); + while (!fScope->functionOf && fScope->nestedIn) + fScope = fScope->nestedIn; + + const Scope* classScope = fScope->functionOf; + if (classScope && classScope->function) + classScope = classScope->function->token->scope(); + + if (classScope && classScope->isClassOrStruct()) + return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn); + return false; + } + if (expr->isOperatorKeyword() && !Token::simpleMatch(expr->next()->astParent(), ".")) + return true; } if (onVar && expr->variable()) { const Variable* var = expr->variable(); @@ -2978,7 +2982,7 @@ bool isThisChanged(const Token* tok, int indirect, const Settings& settings) if (tok->previous()->function()) { return (!tok->previous()->function()->isConst() && !tok->previous()->function()->isStatic()); } - if (!tok->previous()->isKeyword()) { + if (!tok->previous()->isKeyword() || tok->previous()->isOperatorKeyword()) { return true; } } From 68098956fc9e68378391ae0f84c80653040cf9a1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:58:25 +0100 Subject: [PATCH 3/3] Update astutils.cpp --- lib/astutils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index ced367c1e42..7e3c2d16010 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1122,11 +1122,11 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth) const Scope* fScope = expr->scope(); while (!fScope->functionOf && fScope->nestedIn) fScope = fScope->nestedIn; - + const Scope* classScope = fScope->functionOf; if (classScope && classScope->function) classScope = classScope->function->token->scope(); - + if (classScope && classScope->isClassOrStruct()) return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn); return false;