Partial fix for #9407 FN redundant assignment/unreadVariable (#3651)

This commit is contained in:
chrchr-github 2022-02-12 15:32:08 +01:00 committed by GitHub
parent 68b00b3cfc
commit 0a99e3b6fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -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;

View File

@ -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"