loplugin:unusedfields, fix var taking ref

Change-Id: I0ea1f0c7488c140fca9f64de326c6ac588ece925
This commit is contained in:
Noel Grandin
2017-09-20 16:22:27 +02:00
parent d6e8e522a3
commit e8b5ec6590
3 changed files with 28 additions and 7 deletions

View File

@@ -611,6 +611,19 @@ void UnusedFields::checkReadOnly(const FieldDecl* fieldDecl, const Expr* memberE
{
if (!parent)
{
// check if we have an expression like
// int& r = m_field;
auto parentsRange = compiler.getASTContext().getParents(*child);
if (parentsRange.begin() != parentsRange.end())
{
auto varDecl = dyn_cast_or_null<VarDecl>(parentsRange.begin()->get<Decl>());
// The isImplicit() call is to avoid triggering when we see the vardecl which is part of a for-range statement,
// which is of type 'T&&' and also an l-value-ref ?
if (varDecl && !varDecl->isImplicit() && loplugin::TypeCheck(varDecl->getType()).LvalueReference().NonConst())
{
bPotentiallyWrittenTo = true;
}
}
break;
}
if (isa<CXXReinterpretCastExpr>(parent))