Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/fwdanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
const Result &result1 = checkRecursive(expr, tok->tokAt(2), tok->linkAt(1), exprVarIds, local, inInnerClass, depth);
if (result1.type == Result::Type::READ || result1.type == Result::Type::BAILOUT)
return result1;
if (mWhat == What::UnusedValue && result1.type == Result::Type::WRITE && expr->variable() && expr->variable()->isReference())
return result1;
if (mWhat == What::ValueFlow && result1.type == Result::Type::WRITE)
mValueFlowKnown = false;
if (mWhat == What::Reassign && result1.type == Result::Type::BREAK) {
Expand Down Expand Up @@ -530,6 +532,10 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co
addrOf = tok->astOperand1();
else if (Token::simpleMatch(tok, "std :: ref ("))
addrOf = tok->tokAt(3)->astOperand2();
else if (tok->valueType() && tok->valueType()->pointer &&
(Token::Match(tok, "%var% = %var% ;") || Token::Match(tok, "%var% {|( %var% }|)")) &&
Token::Match(expr->previous(), "%varid% [", tok->tokAt(2)->varId()))
addrOf = tok->tokAt(2);
else
continue;

Expand Down
32 changes: 32 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(localvaralias19); // ticket #9828
TEST_CASE(localvaralias20); // ticket #10966
TEST_CASE(localvaralias21);
TEST_CASE(localvaralias22);
TEST_CASE(localvaralias23);
TEST_CASE(localvarasm);
TEST_CASE(localvarstatic);
TEST_CASE(localvarextern);
Expand Down Expand Up @@ -5037,6 +5039,36 @@ class TestUnusedVar : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void localvaralias22() { // #11139
functionVariableUsage("int f() {\n"
" int x[1], *p = x;\n"
" x[0] = 42;\n"
" return *p;\n"
"}\n"
"int g() {\n"
" int x[1], *p{ x };\n"
" x[0] = 42;\n"
" return *p;\n"
"}\n"
"int h() {\n"
" int x[1], *p(x);\n"
" x[0] = 42;\n"
" return *p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void localvaralias23() { // #11817
functionVariableUsage("void f(int& r, bool a, bool b) {\n"
" int& e = r;\n"
" if (a)\n"
" e = 42;\n"
" else if (b)\n"
" e = 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void localvarasm() {

functionVariableUsage("void foo(int &b)\n"
Expand Down