From 235ef0a01e3a2dc396a988d9200a1637831f55b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 18 Dec 2019 19:39:23 +0100 Subject: [PATCH] Fixed #9420 (False positive - redundantInitialization) --- lib/astutils.cpp | 9 +++++++++ test/testother.cpp | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 68d371f4d..a04f93d0d 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1681,6 +1681,15 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const return Result(Result::Type::BAILOUT); } + if (mWhat == What::Reassign && + Token::simpleMatch(tok, ";") && + Token::simpleMatch(tok->astParent(), ";") && + Token::simpleMatch(tok->astParent()->astParent(), "(") && + Token::simpleMatch(tok->astParent()->astParent()->previous(), "for (") && + !isUnchanged(tok, tok->astParent()->astParent()->link(), exprVarIds, local)) + // TODO: This is a quick bailout to avoid FP #9420, there are false negatives (TODO_ASSERT_EQUALS) + return Result(Result::Type::BAILOUT); + if (expr->isName() && Token::Match(tok, "%name% (") && tok->str().find("<") != std::string::npos && tok->str().find(expr->str()) != std::string::npos) return Result(Result::Type::BAILOUT); diff --git a/test/testother.cpp b/test/testother.cpp index 00afe2e15..6bbf2bc37 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6629,6 +6629,19 @@ private: " } while (false);\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void foo(int num) {\n" // #9420 FP + " int a = num;\n" + " for (int b = 0; b < num; a = b++)\n" + " dostuff(a);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void foo(int num) {\n" // #9420 FN + " int a = num;\n" + " for (int b = 0; b < num; a = b++);\n" + "}"); + TODO_ASSERT_EQUALS("error", "", errout.str()); } void redundantVarAssignment_after_switch() {