Skip to content

Commit

Permalink
Fix #12894 FP redundantAssignment with call to operator= (#7012)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Dec 5, 2024
1 parent 30ab5f3 commit d6e7f40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,19 +1117,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();
Expand Down Expand Up @@ -3014,7 +3018,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;
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10534,6 +10534,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
Expand Down

0 comments on commit d6e7f40

Please sign in to comment.