From 0160f80ffec3279ae76083bbc3c52c53d035b326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 25 Nov 2018 10:32:30 +0100 Subject: [PATCH] redundantVarAssignment: avoid FPs when loops are used --- lib/checkother.cpp | 6 ++++++ test/testother.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f215a8517..434ecc4e6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -471,6 +471,12 @@ const Token *CheckOther::checkRedundantAssignmentRecursive(const Token *assign1, return nullptr; } + if (tok->str() == "}" && (tok->scope()->type == Scope::eFor || tok->scope()->type == Scope::eWhile)) { + // TODO: handle loops better + *read = true; + return nullptr; + } + if (Token::Match(tok, "break|continue|return|throw|goto")) { // TODO: handle these better *read = true; diff --git a/test/testother.cpp b/test/testother.cpp index 21658139d..04d289e47 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -162,6 +162,7 @@ private: TEST_CASE(redundantVarAssignment_7133); TEST_CASE(redundantVarAssignment_stackoverflow); TEST_CASE(redundantVarAssignment_lambda); + TEST_CASE(redundantVarAssignment_for); TEST_CASE(redundantMemWrite); TEST_CASE(varFuncNullUB); @@ -6050,6 +6051,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void redundantVarAssignment_for() { + check("void f() {\n" + " char buf[10];\n" + " int i;\n" + " for (i = 0; i < 4; i++)\n" + " buf[i] = 131;\n" + " buf[i] = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void redundantMemWrite() { return; // FIXME: temporary hack