loplugin:useuniqueptr look for DELETEZ expressions

can't believe I've been missing these

Change-Id: If39827e1583cbcedfd5061a5059d6df53be0f9c8
Reviewed-on: https://gerrit.libreoffice.org/53598
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2018-04-24 14:04:53 +02:00
parent 8f2788e6fc
commit a369e6bbd5
2 changed files with 42 additions and 0 deletions

View File

@@ -151,4 +151,15 @@ class Foo12 {
delete m_pbar[i++]; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
}
};
#define DELETEZ( p ) ( delete p,p = NULL )
class Foo13 {
int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
int * m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
~Foo13()
{
if (m_pbar1)
DELETEZ(m_pbar1); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
DELETEZ(m_pbar2); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
}
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */

View File

@@ -58,6 +58,7 @@ private:
void CheckLoopDelete(const CXXMethodDecl*, const Stmt* );
void CheckLoopDelete(const CXXMethodDecl*, const CXXDeleteExpr* );
void CheckDeleteExpr(const CXXMethodDecl*, const CXXDeleteExpr*);
void CheckParenExpr(const CXXMethodDecl*, const ParenExpr*);
void CheckDeleteExpr(const CXXMethodDecl*, const CXXDeleteExpr*,
const MemberExpr*, StringRef message);
};
@@ -101,6 +102,12 @@ void UseUniquePtr::CheckForSimpleDelete(const CXXMethodDecl* methodDecl, const C
CheckDeleteExpr(methodDecl, deleteExpr);
continue;
}
auto parenExpr = dyn_cast<ParenExpr>(*i);
if (parenExpr)
{
CheckParenExpr(methodDecl, parenExpr);
continue;
}
// Check for conditional deletes like:
// if (m_pField != nullptr) delete m_pField;
auto ifStmt = dyn_cast<IfStmt>(*i);
@@ -132,6 +139,13 @@ void UseUniquePtr::CheckForSimpleDelete(const CXXMethodDecl* methodDecl, const C
continue;
}
parenExpr = dyn_cast<ParenExpr>(ifStmt->getThen());
if (parenExpr)
{
CheckParenExpr(methodDecl, parenExpr);
continue;
}
auto ifThenCompoundStmt = dyn_cast<CompoundStmt>(ifStmt->getThen());
if (!ifThenCompoundStmt)
continue;
@@ -140,6 +154,9 @@ void UseUniquePtr::CheckForSimpleDelete(const CXXMethodDecl* methodDecl, const C
auto ifDeleteExpr = dyn_cast<CXXDeleteExpr>(*j);
if (ifDeleteExpr)
CheckDeleteExpr(methodDecl, ifDeleteExpr);
ParenExpr const * parenExpr = dyn_cast<ParenExpr>(*i);
if (parenExpr)
CheckParenExpr(methodDecl, parenExpr);
}
}
}
@@ -159,6 +176,20 @@ void UseUniquePtr::CheckDeleteExpr(const CXXMethodDecl* methodDecl, const CXXDel
"unconditional call to delete on a member, should be using std::unique_ptr");
}
/**
* Look for DELETEZ expressions.
*/
void UseUniquePtr::CheckParenExpr(const CXXMethodDecl* methodDecl, const ParenExpr* parenExpr)
{
auto binaryOp = dyn_cast<BinaryOperator>(parenExpr->getSubExpr());
if (!binaryOp || binaryOp->getOpcode() != BO_Comma)
return;
auto deleteExpr = dyn_cast<CXXDeleteExpr>(binaryOp->getLHS());
if (!deleteExpr)
return;
CheckDeleteExpr(methodDecl, deleteExpr);
}
/**
* Check the delete expression in a destructor.
*/