From 6fef02498c042cecc8048ff744a340e2597b6aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 27 Dec 2018 17:27:51 +0100 Subject: [PATCH] Fixed #7263 (False negative: redundant assignment using +=) --- lib/astutils.cpp | 4 +++- test/testunusedvar.cpp | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e50bcdf5a..fcf6f2c5d 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1191,9 +1191,11 @@ struct FwdAnalysisAllPaths::Result FwdAnalysisAllPaths::checkRecursive(const Tok if (reassign) return Result(Result::Type::WRITE, parent->astParent()); return Result(Result::Type::READ); + } else if (Token::Match(parent->astParent(), "%assign%") && !parent->astParent()->astParent() && parent == parent->astParent()->astOperand1()) { + continue; } else { // TODO: this is a quick bailout - return Result(Result::Type::BAILOUT); + return Result(Result::Type::BAILOUT, parent->astParent()); } } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index cfc46f2b7..8e1985ad5 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -809,7 +809,8 @@ private: " d += code;\n" " }\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'd' is assigned a value that is never used.\n" + "[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", errout.str()); functionVariableUsage("void foo()\n" "{\n" @@ -868,7 +869,8 @@ private: " d += code;\n" " } while(code < 20);\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'd' is assigned a value that is never used.\n" + "[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", errout.str()); functionVariableUsage("void foo()\n" "{\n" @@ -2179,6 +2181,15 @@ private: " status = x;\n" "}"); ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("void f()\n" + "{\n" + " int sum = 0U;\n" + " for (i = 0U; i < 2U; i++)\n" + " sum += 123;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'sum' is assigned a value that is never used.\n" + "[test.cpp:5]: (style) Variable 'sum' is assigned a value that is never used.\n", errout.str()); } void localvaralias1() { @@ -3442,7 +3453,8 @@ private: " int b = 2;\n" " a |= b;\n" "}"); - ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used.\n" + "[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used.\n", errout.str()); functionVariableUsage("void foo() {\n" " int a = 1;\n"