From 79cd601ae758584b11dcdce2ec6dd36fb2245509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 15 Nov 2012 07:48:45 +0100 Subject: [PATCH] Fixed #4356 (False positive at variable initialization) --- lib/checkother.cpp | 5 ++++- test/testother.cpp | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fefb858d5..953f66ef0 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -722,7 +722,10 @@ void CheckOther::checkRedundantAssignment() } it->second = tok; } - varAssignments[tok->varId()] = tok; + if (Token::simpleMatch(tok->tokAt(2), "0 ;")) + varAssignments.erase(tok->varId()); + else + 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 varAssignments[tok->varId()] = tok; diff --git a/test/testother.cpp b/test/testother.cpp index d827d2c97..c1b56f528 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1696,6 +1696,12 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // Ticket #4356 + " int x = 0;\n" // <- ignore assignment with 0 + " x = 3;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void switchRedundantOperationTest() {