Skip to content

Commit b891890

Browse files
committed
Fixed false positive cppcheck-opensource#5466
1 parent 8f4662d commit b891890

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/checkunusedvar.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10561056
else if (tok->isAssignmentOp()) {
10571057
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
10581058
if (tok2->varId()) {
1059-
if (tok2->next()->isAssignmentOp())
1059+
if (tok2->strAt(1) == "=")
10601060
variables.write(tok2->varId(), tok);
1061+
else if (tok2->next()->isAssignmentOp())
1062+
variables.use(tok2->varId(), tok);
10611063
else
10621064
variables.read(tok2->varId(), tok);
10631065
}

test/testunusedvar.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ class TestUnusedVar : public TestFixture {
156156
TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe
157157
TEST_CASE(localvarUnusedGoto); // #4447, #4558 goto
158158

159+
TEST_CASE(chainedAssignment); // #5466
160+
159161
TEST_CASE(crash1);
160162
TEST_CASE(crash2);
161163
TEST_CASE(usingNamespace); // #4585
@@ -3773,6 +3775,16 @@ class TestUnusedVar : public TestFixture {
37733775
ASSERT_EQUALS("", errout.str());
37743776
}
37753777

3778+
void chainedAssignment() {
3779+
// #5466
3780+
functionVariableUsage("void NotUsed(double* pdD, int n) {\n"
3781+
" double sum = 0.0;\n"
3782+
" for (int i = 0; i<n; ++i)\n"
3783+
" pdD[i] = (sum += pdD[i]);\n"
3784+
"}");
3785+
ASSERT_EQUALS("", errout.str());
3786+
}
3787+
37763788
void crash1() {
37773789
functionVariableUsage("SAL_WNODEPRECATED_DECLARATIONS_PUSH\n"
37783790
"void convertToTokenArray() {\n"

0 commit comments

Comments
 (0)