Improved loplugin:redundantcast const_cast handling

Change-Id: I4c24ff5d9d5e74bef2a4040c6308c504282af55d
This commit is contained in:
Stephan Bergmann
2017-06-02 13:31:37 +02:00
parent ff77fdbe29
commit 95645cbf0a
2 changed files with 119 additions and 13 deletions

View File

@@ -37,9 +37,12 @@ bool isVoidPointer(QualType type) {
}
bool isRedundantConstCast(CXXConstCastExpr const * expr) {
return expr->getTypeAsWritten().getCanonicalType().getTypePtr()
== (expr->getSubExprAsWritten()->getType().getCanonicalType()
.getTypePtr());
auto const sub = compat::getSubExprAsWritten(expr);
return
(expr->getType().getCanonicalType()
== sub->getType().getCanonicalType())
&& (expr->getValueKind() != VK_XValue
|| sub->getValueKind() == VK_XValue);
}
bool isArithmeticOp(Expr const * expr) {
@@ -511,11 +514,14 @@ bool RedundantCast::VisitCXXConstCastExpr(CXXConstCastExpr const * expr) {
return true;
}
if (isRedundantConstCast(expr)) {
auto const sub = compat::getSubExprAsWritten(expr);
report(
DiagnosticsEngine::Warning, "redundant const_cast from %0 to %1",
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType()
<< expr->getTypeAsWritten() << expr->getSourceRange();
DiagnosticsEngine::Warning,
"redundant const_cast from %0 %1 to %2 %3", expr->getExprLoc())
<< sub->getType() << printExprValueKind(sub->getValueKind())
<< expr->getTypeAsWritten()
<< printExprValueKind(expr->getValueKind())
<< expr->getSourceRange();
}
return true;
}