diff options
Diffstat (limited to 'compilerplugins/clang/unusedvariablecheck.cxx')
-rw-r--r-- | compilerplugins/clang/unusedvariablecheck.cxx | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/compilerplugins/clang/unusedvariablecheck.cxx b/compilerplugins/clang/unusedvariablecheck.cxx index 1e09645733e0..73529e118bf4 100644 --- a/compilerplugins/clang/unusedvariablecheck.cxx +++ b/compilerplugins/clang/unusedvariablecheck.cxx @@ -16,6 +16,7 @@ #include "compat.hxx" #include "check.hxx" #include "unusedvariablecheck.hxx" +#include "plugin.hxx" namespace loplugin { @@ -57,6 +58,10 @@ bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var ) auto type = var->getType(); bool check = loplugin::isExtraWarnUnusedType(type); + if (!check) + check = isUnusedSmartPointer(var); + + // this chunk of logic generates false+, which is why we don't leave it on /* if (!check && type->isRecordType()) @@ -88,6 +93,22 @@ bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var ) return true; } +bool UnusedVariableCheck::isUnusedSmartPointer( const VarDecl* var ) + { + // if we have a var of smart-pointer type, and that var is both uninitialised and + // not referenced, then we can remove it + if (!isSmartPointerType(var->getType())) + return false; + + if (!var->hasInit()) + return true; + + auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(var->getInit()); + if (!cxxConstructExpr) + return false; + return cxxConstructExpr->getConstructor()->isDefaultConstructor(); + } + static Plugin::Registration< UnusedVariableCheck > unusedvariablecheck( "unusedvariablecheck" ); } // namespace |