Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12894 FP redundantAssignment with call to operator= #7012

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading