Fixed #7134 (False positive redundantAssignment - assignment to local variable of unknown type)

This commit is contained in:
Daniel Marjamäki 2016-08-29 03:48:08 +02:00
parent 18cab009c1
commit 939b42df06
2 changed files with 20 additions and 11 deletions

View File

@ -467,8 +467,9 @@ static bool checkExceptionHandling(const Token* tok)
void CheckOther::checkRedundantAssignment() void CheckOther::checkRedundantAssignment()
{ {
const bool printPerformance = _settings->isEnabled("performance"); const bool printPerformance = _settings->isEnabled("performance");
const bool printStyle = _settings->isEnabled("style");
const bool printWarning = _settings->isEnabled("warning"); const bool printWarning = _settings->isEnabled("warning");
if (!printWarning && !printPerformance) if (!printWarning && !printPerformance && !printStyle)
return; return;
const bool printInconclusive = _settings->inconclusive; const bool printInconclusive = _settings->inconclusive;
@ -597,15 +598,24 @@ void CheckOther::checkRedundantAssignment()
} }
} }
} }
if (error) { if (error) {
if (printWarning && scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok)) if (printWarning && scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok))
redundantAssignmentInSwitchError(it->second, tok, eq->astOperand1()->expressionString()); redundantAssignmentInSwitchError(it->second, tok, eq->astOperand1()->expressionString());
else if (printPerformance) { else if (printStyle) {
// See #7133 // c++, unknown type => assignment might have additional side effects
const bool possibleSideEffects(_tokenizer->isCPP() && !tok->valueType());
// TODO nonlocal variables are not tracked entirely.
const bool nonlocal = it->second->variable() && nonLocalVolatile(it->second->variable()); const bool nonlocal = it->second->variable() && nonLocalVolatile(it->second->variable());
if (printInconclusive || !nonlocal) // report inconclusive only when requested
// Warnings are inconclusive if there are possible side effects or if variable is not
// tracked perfectly.
const bool inconclusive = possibleSideEffects | nonlocal;
if (printInconclusive || !inconclusive)
if (_tokenizer->isC() || checkExceptionHandling(tok)) // see #6555 to see how exception handling might have an impact if (_tokenizer->isC() || checkExceptionHandling(tok)) // see #6555 to see how exception handling might have an impact
redundantAssignmentError(it->second, tok, eq->astOperand1()->expressionString(), nonlocal); // Inconclusive for non-local variables redundantAssignmentError(it->second, tok, eq->astOperand1()->expressionString(), inconclusive);
} }
} }
it->second = tok; it->second = tok;

View File

@ -5041,7 +5041,7 @@ private:
" if (memptr)\n" " if (memptr)\n"
" memptr = 0;\n" " memptr = 0;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'memptr' is reassigned a value before the old one has been used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style, inconclusive) Variable 'memptr' is reassigned a value before the old one has been used if variable is no semaphore variable.\n", errout.str());
} }
void redundantVarAssignment_7133() { void redundantVarAssignment_7133() {
@ -5067,7 +5067,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void ConvertBitmapData(sal_uInt16 nDestBits) {\n" check("void ConvertBitmapData(sal_uInt16 nDestBits) {\n"
"BitmapBuffer aSrcBuf;\n" " BitmapBuffer aSrcBuf;\n"
" aSrcBuf.mnBitCount = nSrcBits;\n" " aSrcBuf.mnBitCount = nSrcBits;\n"
" BitmapBuffer aDstBuf;\n" " BitmapBuffer aDstBuf;\n"
" aSrcBuf.mnBitCount = nDestBits;\n" " aSrcBuf.mnBitCount = nDestBits;\n"
@ -5075,14 +5075,13 @@ private:
"}", "test.c"); "}", "test.c");
ASSERT_EQUALS("[test.c:3] -> [test.c:5]: (style) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used.\n", errout.str()); ASSERT_EQUALS("[test.c:3] -> [test.c:5]: (style) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used.\n", errout.str());
check("void ConvertBitmapData(sal_uInt16 nDestBits) {\n" check("void ConvertBitmapData(sal_uInt16 nDestBits) {\n"
"BitmapBuffer aSrcBuf;\n" " BitmapBuffer aSrcBuf;\n"
" aSrcBuf.mnBitCount = nSrcBits;\n" " aSrcBuf.mnBitCount = nSrcBits;\n"
" BitmapBuffer aDstBuf;\n" " BitmapBuffer aDstBuf;\n"
" aSrcBuf.mnBitCount = nDestBits;\n" " aSrcBuf.mnBitCount = nDestBits;\n"
" bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects );\n" " bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects );\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style, inconclusive) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used.\n", 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",
"[test.cpp:3] -> [test.cpp:5]: (style) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used.\n",
errout.str()); errout.str());
} }