Skip to content

Commit d6e7f40

Browse files
Fix #12894 FP redundantAssignment with call to operator= (danmar#7012)
1 parent 30ab5f3 commit d6e7f40

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/astutils.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,19 +1117,23 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
11171117
++depth;
11181118

11191119
// calling nonstatic method?
1120-
if (Token::Match(expr, "%name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) {
1121-
// is it a method of this?
1122-
const Scope* fScope = expr->scope();
1123-
while (!fScope->functionOf && fScope->nestedIn)
1124-
fScope = fScope->nestedIn;
1125-
1126-
const Scope* classScope = fScope->functionOf;
1127-
if (classScope && classScope->function)
1128-
classScope = classScope->function->token->scope();
1129-
1130-
if (classScope && classScope->isClassOrStruct())
1131-
return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn);
1132-
return false;
1120+
if (Token::Match(expr, "%name% (")) {
1121+
if (expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) {
1122+
// is it a method of this?
1123+
const Scope* fScope = expr->scope();
1124+
while (!fScope->functionOf && fScope->nestedIn)
1125+
fScope = fScope->nestedIn;
1126+
1127+
const Scope* classScope = fScope->functionOf;
1128+
if (classScope && classScope->function)
1129+
classScope = classScope->function->token->scope();
1130+
1131+
if (classScope && classScope->isClassOrStruct())
1132+
return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn);
1133+
return false;
1134+
}
1135+
if (expr->isOperatorKeyword() && !Token::simpleMatch(expr->next()->astParent(), "."))
1136+
return true;
11331137
}
11341138
if (onVar && expr->variable()) {
11351139
const Variable* var = expr->variable();
@@ -3014,7 +3018,7 @@ bool isThisChanged(const Token* tok, int indirect, const Settings& settings)
30143018
if (tok->previous()->function()) {
30153019
return (!tok->previous()->function()->isConst() && !tok->previous()->function()->isStatic());
30163020
}
3017-
if (!tok->previous()->isKeyword()) {
3021+
if (!tok->previous()->isKeyword() || tok->previous()->isOperatorKeyword()) {
30183022
return true;
30193023
}
30203024
}

test/testother.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10534,6 +10534,23 @@ class TestOther : public TestFixture {
1053410534
" return j;\n"
1053510535
"}\n");
1053610536
ASSERT_EQUALS("", errout_str());
10537+
10538+
check("struct S {\n" // #12894
10539+
" std::string a;\n"
10540+
" void f(const S& s);\n"
10541+
" void g(const S& s);\n"
10542+
"};\n"
10543+
"void S::f(const S& s) {\n"
10544+
" std::string x = a;\n"
10545+
" this->operator=(s);\n"
10546+
" a = x;\n"
10547+
"}\n"
10548+
"void S::g(const S& s) {\n"
10549+
" std::string x = a;\n"
10550+
" operator=(s);\n"
10551+
" a = x;\n"
10552+
"}\n");
10553+
ASSERT_EQUALS("", errout_str());
1053710554
}
1053810555

1053910556
void varFuncNullUB() { // #4482

0 commit comments

Comments
 (0)