From 0a99e3b6fc94b86991b38519e77a9ad690e76f80 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 12 Feb 2022 15:32:08 +0100 Subject: [PATCH] Partial fix for #9407 FN redundant assignment/unreadVariable (#3651) --- lib/astutils.cpp | 7 +++++-- test/testunusedvar.cpp | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8f264213a..bf2eb1817 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -3339,8 +3339,11 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co if (tok->function() && tok->function()->getArgumentVar(argnr) && !tok->function()->getArgumentVar(argnr)->isReference() && !tok->function()->isConst()) continue; for (const Token *subexpr = expr; subexpr; subexpr = subexpr->astOperand1()) { - if (isSameExpression(mCpp, macro, subexpr, args[argnr], mLibrary, pure, followVar)) - return true; + if (isSameExpression(mCpp, macro, subexpr, args[argnr], mLibrary, pure, followVar)) { + const Scope* scope = expr->scope(); // if there is no other variable, assume no aliasing + if (scope->varlist.size() > 1) + return true; + } } } continue; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 2b7cf4e0b..57ffca263 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -126,6 +126,7 @@ private: TEST_CASE(localvar58); // #9901 - increment false positive TEST_CASE(localvar59); // #9737 TEST_CASE(localvar60); + TEST_CASE(localvar61); // #9407 TEST_CASE(localvarloops); // loops TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 @@ -3292,6 +3293,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvar61() { // #9407 + functionVariableUsage("void g(int& i);\n" + "void f() {\n" + " int var = 0;\n" + " g(var);\n" + " var = 2;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'var' is assigned a value that is never used.\n", errout.str()); + } + void localvarloops() { // loops functionVariableUsage("void fun(int c) {\n" @@ -3318,7 +3329,7 @@ private: " if (y) { x=10; break; }\n" " }\n" "}"); - ASSERT_EQUALS("", errout.str()); // TODO : in this special case we can ignore that x is aliased. x is local and there are no function calls after the assignment + ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str()); functionVariableUsage("void fun() {\n" " int x = 0;\n"