1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
16 #include <clang/AST/CXXInheritance.h>
21 look for unnecessary blocks that just catch and rethrow:
24 } catch (exception const &) {
31 class UnnecessaryCatchThrow
:
32 public loplugin::FilteringPlugin
<UnnecessaryCatchThrow
>
35 explicit UnnecessaryCatchThrow(loplugin::InstantiationData
const & data
): FilteringPlugin(data
) {}
37 virtual void run() override
39 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
42 bool VisitCXXTryStmt(CXXTryStmt
const *);
45 bool UnnecessaryCatchThrow::VisitCXXTryStmt(CXXTryStmt
const * tryStmt
)
47 if (ignoreLocation(tryStmt
))
49 if (tryStmt
->getNumHandlers() != 1)
51 auto catchStmt
= tryStmt
->getHandler(0);
52 auto compoundStmt
= dyn_cast
<CompoundStmt
>(catchStmt
->getHandlerBlock());
53 if (!compoundStmt
|| compoundStmt
->size() != 1)
55 auto throwExpr
= dyn_cast
<CXXThrowExpr
>(compoundStmt
->body_front());
58 auto subExpr
= throwExpr
->getSubExpr();
61 if (auto cxxConstructExpr
= dyn_cast
<CXXConstructExpr
>(subExpr
)) {
62 if (!cxxConstructExpr
->getConstructor()->isCopyConstructor())
64 if (!cxxConstructExpr
->getConstructor()->getParent()->hasAttr
<FinalAttr
>())
66 if (cxxConstructExpr
->getNumArgs() != 1)
68 subExpr
= cxxConstructExpr
->getArg(0);
70 auto declRefExpr
= dyn_cast
<DeclRefExpr
>(subExpr
->IgnoreImpCasts());
73 if (declRefExpr
->getDecl() != catchStmt
->getExceptionDecl())
77 report( DiagnosticsEngine::Warning
, "unnecessary catch and throw",
78 compat::getBeginLoc(catchStmt
))
79 << catchStmt
->getSourceRange();
84 loplugin::Plugin::Registration
< UnnecessaryCatchThrow
> X("unnecessarycatchthrow");
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */