Skip to content

Commit 0a99e3b

Browse files
Partial fix for #9407 FN redundant assignment/unreadVariable (danmar#3651)
1 parent 68b00b3 commit 0a99e3b

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/astutils.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,8 +3339,11 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co
33393339
if (tok->function() && tok->function()->getArgumentVar(argnr) && !tok->function()->getArgumentVar(argnr)->isReference() && !tok->function()->isConst())
33403340
continue;
33413341
for (const Token *subexpr = expr; subexpr; subexpr = subexpr->astOperand1()) {
3342-
if (isSameExpression(mCpp, macro, subexpr, args[argnr], mLibrary, pure, followVar))
3343-
return true;
3342+
if (isSameExpression(mCpp, macro, subexpr, args[argnr], mLibrary, pure, followVar)) {
3343+
const Scope* scope = expr->scope(); // if there is no other variable, assume no aliasing
3344+
if (scope->varlist.size() > 1)
3345+
return true;
3346+
}
33443347
}
33453348
}
33463349
continue;

test/testunusedvar.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class TestUnusedVar : public TestFixture {
126126
TEST_CASE(localvar58); // #9901 - increment false positive
127127
TEST_CASE(localvar59); // #9737
128128
TEST_CASE(localvar60);
129+
TEST_CASE(localvar61); // #9407
129130
TEST_CASE(localvarloops); // loops
130131
TEST_CASE(localvaralias1);
131132
TEST_CASE(localvaralias2); // ticket #1637
@@ -3292,6 +3293,16 @@ class TestUnusedVar : public TestFixture {
32923293
ASSERT_EQUALS("", errout.str());
32933294
}
32943295

3296+
void localvar61() { // #9407
3297+
functionVariableUsage("void g(int& i);\n"
3298+
"void f() {\n"
3299+
" int var = 0;\n"
3300+
" g(var);\n"
3301+
" var = 2;\n"
3302+
"}\n");
3303+
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'var' is assigned a value that is never used.\n", errout.str());
3304+
}
3305+
32953306
void localvarloops() {
32963307
// loops
32973308
functionVariableUsage("void fun(int c) {\n"
@@ -3318,7 +3329,7 @@ class TestUnusedVar : public TestFixture {
33183329
" if (y) { x=10; break; }\n"
33193330
" }\n"
33203331
"}");
3321-
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
3332+
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
33223333

33233334
functionVariableUsage("void fun() {\n"
33243335
" int x = 0;\n"

0 commit comments

Comments
 (0)