From 4cef2e94e78e572414d4a83ca228559dd4259454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 Nov 2018 18:42:13 +0100 Subject: [PATCH] Fixed #8368 (FP redundantAssignment - assignment has unknown side-effects) --- lib/checkother.cpp | 7 +++++-- test/testother.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) 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() {