diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 0f2ba6200..89f0bf5f7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -619,8 +619,11 @@ void CheckOther::checkRedundantAssignment() if (printWarning && scope.type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok)) redundantAssignmentInSwitchError(it->second, tok, eq->astOperand1()->expressionString()); else if (printStyle) { - // c++, unknown type => assignment might have additional side effects - const bool possibleSideEffects(mTokenizer->isCPP() && !tok->valueType()); + // c++ and (unknown type or overloaded assignment operator) => assignment might have additional side effects + const bool possibleSideEffects = mTokenizer->isCPP() && + (!tok->valueType() || + (tok->valueType()->typeScope && + tok->valueType()->typeScope->functionMap.count("operator="))); // TODO nonlocal variables are not tracked entirely. const bool nonlocal = it->second->variable() && nonLocalVolatile(it->second->variable()); diff --git a/test/testother.cpp b/test/testother.cpp index 459904d12..a4b716e5f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5993,6 +5993,13 @@ private: ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style, inconclusive) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used if variable is no semaphore variable.\n", errout.str()); + check("class C { void operator=(int x); };\n" // #8368 - assignment operator might have side effects => inconclusive + "void f() {\n" + " C c;\n" + " c = x;\n" + " c = x;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style, inconclusive) Variable 'c' is reassigned a value before the old one has been used if variable is no semaphore variable.\n", errout.str()); } void redundantVarAssignment_stackoverflow() {