Fixed #8368 (FP redundantAssignment - assignment has unknown side-effects)

This commit is contained in:
Daniel Marjamäki 2018-11-10 18:42:13 +01:00
parent 69e6e11844
commit 4cef2e94e7
2 changed files with 12 additions and 2 deletions

View File

@ -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());

View File

@ -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() {