From bde8b4316ece8fc8d296e30547f79f0eefa6fe91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 May 2021 13:31:16 +0200 Subject: [PATCH] Uninitialized variables; Fixed FP in range based for loop --- lib/checkuninitvar.cpp | 7 +++++++ test/testuninitvar.cpp | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 66d4fd70b..9bd084595 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1132,6 +1132,13 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, return nullptr; } + // LHS in range for loop: + if (Token::simpleMatch(valueExpr->astParent(), ":") && + astIsLhs(valueExpr) && + valueExpr->astParent()->astParent() && + Token::simpleMatch(valueExpr->astParent()->astParent()->previous(), "for (")) + return nullptr; + // Stream read/write // FIXME this code is a hack!! if (mTokenizer->isCPP() && Token::Match(valueExpr->astParent(), "<<|>>")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 9aaa024c1..72a684aac 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -4121,14 +4121,20 @@ private: ASSERT_EQUALS("", errout.str()); } - void uninitvar_rangeBasedFor() { // #7078 - checkUninitVar("void function(Entry& entry) {\n" + void uninitvar_rangeBasedFor() { + checkUninitVar("void function(Entry& entry) {\n" // #7078 " for (auto* expr : entry.exprs) {\n" " expr->operate();\n" " expr->operate();\n" " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar("void f() {\n" + " int *item;\n" + " for (item: itemList) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void uninitvar_static() { // #8734