Skip to content

Commit 939b42d

Browse files
committed
Fixed danmar#7134 (False positive redundantAssignment - assignment to local variable of unknown type)
1 parent 18cab00 commit 939b42d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lib/checkother.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,9 @@ static bool checkExceptionHandling(const Token* tok)
467467
void CheckOther::checkRedundantAssignment()
468468
{
469469
const bool printPerformance = _settings->isEnabled("performance");
470+
const bool printStyle = _settings->isEnabled("style");
470471
const bool printWarning = _settings->isEnabled("warning");
471-
if (!printWarning && !printPerformance)
472+
if (!printWarning && !printPerformance && !printStyle)
472473
return;
473474

474475
const bool printInconclusive = _settings->inconclusive;
@@ -597,15 +598,24 @@ void CheckOther::checkRedundantAssignment()
597598
}
598599
}
599600
}
601+
600602
if (error) {
601603
if (printWarning && scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok))
602604
redundantAssignmentInSwitchError(it->second, tok, eq->astOperand1()->expressionString());
603-
else if (printPerformance) {
604-
// See #7133
605+
else if (printStyle) {
606+
// c++, unknown type => assignment might have additional side effects
607+
const bool possibleSideEffects(_tokenizer->isCPP() && !tok->valueType());
608+
609+
// TODO nonlocal variables are not tracked entirely.
605610
const bool nonlocal = it->second->variable() && nonLocalVolatile(it->second->variable());
606-
if (printInconclusive || !nonlocal) // report inconclusive only when requested
611+
612+
// Warnings are inconclusive if there are possible side effects or if variable is not
613+
// tracked perfectly.
614+
const bool inconclusive = possibleSideEffects | nonlocal;
615+
616+
if (printInconclusive || !inconclusive)
607617
if (_tokenizer->isC() || checkExceptionHandling(tok)) // see #6555 to see how exception handling might have an impact
608-
redundantAssignmentError(it->second, tok, eq->astOperand1()->expressionString(), nonlocal); // Inconclusive for non-local variables
618+
redundantAssignmentError(it->second, tok, eq->astOperand1()->expressionString(), inconclusive);
609619
}
610620
}
611621
it->second = tok;

test/testother.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5041,7 +5041,7 @@ class TestOther : public TestFixture {
50415041
" if (memptr)\n"
50425042
" memptr = 0;\n"
50435043
"}");
5044-
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());
5044+
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());
50455045
}
50465046

50475047
void redundantVarAssignment_7133() {
@@ -5067,23 +5067,22 @@ class TestOther : public TestFixture {
50675067
ASSERT_EQUALS("", errout.str());
50685068

50695069
check("void ConvertBitmapData(sal_uInt16 nDestBits) {\n"
5070-
"BitmapBuffer aSrcBuf;\n"
5070+
" BitmapBuffer aSrcBuf;\n"
50715071
" aSrcBuf.mnBitCount = nSrcBits;\n"
50725072
" BitmapBuffer aDstBuf;\n"
50735073
" aSrcBuf.mnBitCount = nDestBits;\n"
50745074
" bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects );\n"
50755075
"}", "test.c");
50765076
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());
50775077
check("void ConvertBitmapData(sal_uInt16 nDestBits) {\n"
5078-
"BitmapBuffer aSrcBuf;\n"
5078+
" BitmapBuffer aSrcBuf;\n"
50795079
" aSrcBuf.mnBitCount = nSrcBits;\n"
50805080
" BitmapBuffer aDstBuf;\n"
50815081
" aSrcBuf.mnBitCount = nDestBits;\n"
50825082
" bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects );\n"
50835083
"}");
5084-
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",
5085-
"[test.cpp:3] -> [test.cpp:5]: (style) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used.\n",
5086-
errout.str());
5084+
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",
5085+
errout.str());
50875086

50885087
}
50895088

0 commit comments

Comments
 (0)