From fb480ebb0a0099237532a14a9c606c1f9f1dba90 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 9 May 2013 15:39:33 +0200 Subject: [PATCH] Now really fixed #4604. --- lib/checkother.cpp | 4 +--- test/testother.cpp | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3d3e2022d..87f6d7e5d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -664,9 +664,7 @@ void CheckOther::checkRedundantAssignment() } it->second = tok; } - if (Token::simpleMatch(tok->tokAt(2), "0 ;")) - varAssignments.erase(tok->varId()); - else + if (!Token::simpleMatch(tok->tokAt(2), "0 ;") || (tok->variable() && tok->variable()->nameToken() != tok->tokAt(-2))) varAssignments[tok->varId()] = tok; memAssignments.erase(tok->varId()); } else if (tok->next()->type() == Token::eIncDecOp || (tok->previous()->type() == Token::eIncDecOp && !Token::Match(tok->next(), ".|[|("))) { // Variable incremented/decremented diff --git a/test/testother.cpp b/test/testother.cpp index b9f52a91d..315e97297 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1756,12 +1756,6 @@ private: " }\n" "}", 0, false, false, false, false); ASSERT_EQUALS("", errout.str()); - - check("void f() {\n" // Ticket #4356 - " int x = 0;\n" // <- ignore assignment with 0 - " x = 3;\n" - "}", 0, false, false, false, false); - ASSERT_EQUALS("", errout.str()); } void switchRedundantOperationTest() { @@ -5803,6 +5797,24 @@ private: " return x + 1;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // Ticket #4356 + " int x = 0;\n" // <- ignore assignment with 0 + " x = 3;\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " int i = 54;\n" + " i = 0;\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (performance) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str()); + + check("void f() {\n" + " int i = 54;\n" + " i = 1;\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (performance) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str()); } void redundantMemWrite() { @@ -6165,4 +6177,4 @@ private: } }; -REGISTER_TEST(TestOther) \ No newline at end of file +REGISTER_TEST(TestOther)