From 404b82fc216f89f21a67664b6565c1a779bced37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 14 May 2021 19:07:30 +0200 Subject: [PATCH] unused var; fix todo test cases, redundant assignment of array in loop --- lib/astutils.cpp | 2 ++ test/testunusedvar.cpp | 17 +++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 2eaf3d932..f67edc54a 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2784,6 +2784,8 @@ std::set FwdAnalysis::getExprVarIds(const Token* expr, bool* localOu bool unknownVarId = false; visitAstNodes(expr, [&](const Token *tok) { + if (tok->str() == "[" && mWhat == What::UnusedValue) + return ChildrenToVisit::op1; if (tok->varId() == 0 && tok->isName() && tok->previous()->str() != ".") { // unknown variable unknownVarId = true; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index a95e337f1..ee8ca688a 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -2177,22 +2177,27 @@ private: } void localvar6() { - functionVariableUsage("void foo()\n" - "{\n" + functionVariableUsage("void foo() {\n" " int b[10];\n" " for (int i=0;i<10;++i)\n" " b[i] = 0;\n" "}"); - // TODO ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b' is assigned a value that is never used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b[i]' is assigned a value that is never used.\n", errout.str()); - functionVariableUsage("void foo()\n" - "{\n" + functionVariableUsage("void foo() {\n" " int a = 0;\n" " int b[10];\n" " for (int i=0;i<10;++i)\n" " b[i] = ++a;\n" "}"); - // TODO ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b[i]' is assigned a value that is never used.\n", errout.str()); + + functionVariableUsage("void foo() {\n" + " int b[10];\n" + " for (int i=0;i<10;++i)\n" + " *(b+i) = 0;\n" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Variable '*(b+i)' is assigned a value that is never used.\n", "", errout.str()); } void localvar8() {