Skip to content

Commit 235ef0a

Browse files
committed
Fixed #9420 (False positive - redundantInitialization)
1 parent 7977bbf commit 235ef0a

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lib/astutils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,15 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
16811681
return Result(Result::Type::BAILOUT);
16821682
}
16831683

1684+
if (mWhat == What::Reassign &&
1685+
Token::simpleMatch(tok, ";") &&
1686+
Token::simpleMatch(tok->astParent(), ";") &&
1687+
Token::simpleMatch(tok->astParent()->astParent(), "(") &&
1688+
Token::simpleMatch(tok->astParent()->astParent()->previous(), "for (") &&
1689+
!isUnchanged(tok, tok->astParent()->astParent()->link(), exprVarIds, local))
1690+
// TODO: This is a quick bailout to avoid FP #9420, there are false negatives (TODO_ASSERT_EQUALS)
1691+
return Result(Result::Type::BAILOUT);
1692+
16841693
if (expr->isName() && Token::Match(tok, "%name% (") && tok->str().find("<") != std::string::npos && tok->str().find(expr->str()) != std::string::npos)
16851694
return Result(Result::Type::BAILOUT);
16861695

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6629,6 +6629,19 @@ class TestOther : public TestFixture {
66296629
" } while (false);\n"
66306630
"}");
66316631
ASSERT_EQUALS("", errout.str());
6632+
6633+
check("void foo(int num) {\n" // #9420 FP
6634+
" int a = num;\n"
6635+
" for (int b = 0; b < num; a = b++)\n"
6636+
" dostuff(a);\n"
6637+
"}");
6638+
ASSERT_EQUALS("", errout.str());
6639+
6640+
check("void foo(int num) {\n" // #9420 FN
6641+
" int a = num;\n"
6642+
" for (int b = 0; b < num; a = b++);\n"
6643+
"}");
6644+
TODO_ASSERT_EQUALS("error", "", errout.str());
66326645
}
66336646

66346647
void redundantVarAssignment_after_switch() {

0 commit comments

Comments
 (0)