From 0767284ca13ffd660d1690b54b31787ba59ed5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 25 Jul 2016 07:35:33 +0200 Subject: [PATCH] Fixed #7630 (FP: dead store, modifying array, index var modified) --- lib/checkother.cpp | 19 +++++++++++++++++-- test/testother.cpp | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 919157ec4..0b295163b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -501,9 +501,24 @@ void CheckOther::checkRedundantAssignment() } else if (tok->tokType() == Token::eVariable && !Token::Match(tok, "%name% (")) { const Token *eq = nullptr; for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { - if (Token::Match(tok2, "[([]")) + if (Token::Match(tok2, "[([]")) { + // bail out if there is a variable in rhs - we only track 1 variable + bool bailout = false; + for (const Token *tok3 = tok2->link(); tok3 != tok2; tok3 = tok3->previous()) { + if (tok3->varId()) { + if (!tok3->variable()) + bailout = true; + else { + const Variable *var = tok3->variable(); + if (!var->isConst() || var->isReference() || var->isPointer()) + bailout = true; + } + } + } + if (bailout) + break; tok2 = tok2->link(); - else if (Token::Match(tok2, "[)];,]")) + } else if (Token::Match(tok2, "[)];,]")) break; else if (tok2->str() == "=") { eq = tok2; diff --git a/test/testother.cpp b/test/testother.cpp index d9043f825..b194ff7b7 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4577,6 +4577,21 @@ private: "}", nullptr, false, false, false); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'i[2]' is reassigned a value before the old one has been used.\n", errout.str()); + check("void f(int x) {\n" + " int i[10];\n" + " i[x] = 1;\n" + " x=1;\n" + " i[x] = 1;\n" + "}", nullptr, false, false, false); + ASSERT_EQUALS("", errout.str()); + + check("void f(const int x) {\n" + " int i[10];\n" + " i[x] = 1;\n" + " i[x] = 1;\n" + "}", nullptr, false, false, false); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'i[x]' is reassigned a value before the old one has been used.\n", errout.str()); + // Testing different types check("void f() {\n" " Foo& bar = foo();\n"