summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2018-06-22 11:56:37 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-09-13 11:15:33 +0200
commit61e94390a3b86024b76c3418fe8c5e517c5c5123 (patch)
treecf701ad41c465540c90c6548716180bdd44e278f
parentdfb18a0557d5a897f443fd1f1d617365f6ae134a (diff)
new loplugin simplifyconstruct
no need to init smart pointers with nullptr, they all have default constructors that do this already Change-Id: Ief20c060daa0def8c1aa82f1cf8dc4bc696761e9 Reviewed-on: https://gerrit.libreoffice.org/59818 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r--compilerplugins/clang/simplifyconstruct.cxx68
-rw-r--r--compilerplugins/clang/test/simplifyconstruct.cxx30
-rw-r--r--sdext/source/pdfimport/inc/genericelements.hxx3
-rw-r--r--sdext/source/pdfimport/pdfparse/pdfentries.cxx3
-rw-r--r--solenv/CompilerTest_compilerplugins_clang.mk1
-rw-r--r--writerperfect/source/common/WPXSvInputStream.cxx2
6 files changed, 101 insertions, 6 deletions
diff --git a/compilerplugins/clang/simplifyconstruct.cxx b/compilerplugins/clang/simplifyconstruct.cxx
new file mode 100644
index 000000000000..2a80cdefe4b2
--- /dev/null
+++ b/compilerplugins/clang/simplifyconstruct.cxx
@@ -0,0 +1,68 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <memory>
+#include <cassert>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <set>
+#include "plugin.hxx"
+#include "check.hxx"
+
+namespace
+{
+class SimplifyConstruct : public RecursiveASTVisitor<SimplifyConstruct>, public loplugin::Plugin
+{
+public:
+ explicit SimplifyConstruct(loplugin::InstantiationData const& data)
+ : Plugin(data)
+ {
+ }
+
+ virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+ bool VisitCXXConstructExpr(CXXConstructExpr const*);
+
+ // ignore some contexts within which nullptr is fine
+ bool TraverseReturnStmt(ReturnStmt*) { return true; }
+ bool TraverseInitListExpr(InitListExpr*) { return true; }
+ bool TraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr*) { return true; }
+};
+
+bool SimplifyConstruct::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
+{
+ if (ignoreLocation(constructExpr))
+ return true;
+ auto tc = loplugin::TypeCheck(constructExpr->getType());
+ if (!tc.Class("unique_ptr").StdNamespace() && !tc.Class("shared_ptr").StdNamespace()
+ && !tc.Class("SvRef").Namespace("tools").GlobalNamespace()
+ && !tc.Class("Reference").Namespace("rtl").GlobalNamespace()
+ && !tc.Class("Reference")
+ .Namespace("uno")
+ .Namespace("star")
+ .Namespace("sun")
+ .Namespace("com")
+ .GlobalNamespace())
+ return true;
+ if (constructExpr->getNumArgs() > 0
+ && isa<CXXNullPtrLiteralExpr>(constructExpr->getArg(0)->IgnoreParenImpCasts()))
+ {
+ report(DiagnosticsEngine::Warning,
+ "no need to explicitly init this with nullptr, just use default constructor",
+ constructExpr->getSourceRange().getBegin())
+ << constructExpr->getSourceRange();
+ }
+ return true;
+}
+
+loplugin::Plugin::Registration<SimplifyConstruct> X("simplifyconstruct", true);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/simplifyconstruct.cxx b/compilerplugins/clang/test/simplifyconstruct.cxx
new file mode 100644
index 000000000000..ce8a70aab272
--- /dev/null
+++ b/compilerplugins/clang/test/simplifyconstruct.cxx
@@ -0,0 +1,30 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <memory>
+#include <rtl/ref.hxx>
+
+struct Foo
+{
+ void acquire();
+ void release();
+};
+class Foo16
+{
+ std::unique_ptr<int> m_pbar1;
+ rtl::Reference<Foo> m_pbar2;
+ Foo16()
+ : m_pbar1(nullptr)
+ // expected-error@-1 {{no need to explicitly init this with nullptr, just use default constructor [loplugin:simplifyconstruct]}}
+ , m_pbar2(nullptr)
+ // expected-error@-1 {{no need to explicitly init this with nullptr, just use default constructor [loplugin:simplifyconstruct]}}
+ {
+ }
+};
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/sdext/source/pdfimport/inc/genericelements.hxx b/sdext/source/pdfimport/inc/genericelements.hxx
index fb3fcf93703d..59defa1588e7 100644
--- a/sdext/source/pdfimport/inc/genericelements.hxx
+++ b/sdext/source/pdfimport/inc/genericelements.hxx
@@ -244,8 +244,7 @@ namespace pdfi
protected:
PageElement( Element* pParent, sal_Int32 nPageNr )
: Element( pParent ), PageNumber( nPageNr ), Hyperlinks(),
- TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 ),
- HeaderElement( nullptr ), FooterElement( nullptr )
+ TopMargin( 0.0 ), BottomMargin( 0.0 ), LeftMargin( 0.0 ), RightMargin( 0.0 )
{}
private:
// helper method for resolveHyperlinks
diff --git a/sdext/source/pdfimport/pdfparse/pdfentries.cxx b/sdext/source/pdfimport/pdfparse/pdfentries.cxx
index a0c15c9d14ef..5fed8e18d102 100644
--- a/sdext/source/pdfimport/pdfparse/pdfentries.cxx
+++ b/sdext/source/pdfimport/pdfparse/pdfentries.cxx
@@ -95,8 +95,7 @@ using namespace pdfparse;
EmitContext::EmitContext( const PDFContainer* pTop ) :
m_bDeflate( false ),
- m_bDecrypt( false ),
- m_pImplData( nullptr )
+ m_bDecrypt( false )
{
if( pTop )
m_pImplData.reset( new EmitImplData( pTop ) );
diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk
index ad9b1e3f5192..f56fd772e806 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -49,6 +49,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
compilerplugins/clang/test/salunicodeliteral \
compilerplugins/clang/test/shouldreturnbool \
compilerplugins/clang/test/simplifybool \
+ compilerplugins/clang/test/simplifyconstruct \
compilerplugins/clang/test/simplifydynamiccast \
compilerplugins/clang/test/stringbuffer \
compilerplugins/clang/test/stringconstant \
diff --git a/writerperfect/source/common/WPXSvInputStream.cxx b/writerperfect/source/common/WPXSvInputStream.cxx
index f4d4a5593229..a2692bcea395 100644
--- a/writerperfect/source/common/WPXSvInputStream.cxx
+++ b/writerperfect/source/common/WPXSvInputStream.cxx
@@ -460,8 +460,6 @@ WPXSvInputStreamImpl::WPXSvInputStreamImpl(Reference<XInputStream> const& xStrea
: mxStream(xStream)
, mxSeekable(xStream, UNO_QUERY)
, maData(0)
- , mpOLEStorage(nullptr)
- , mpZipStorage(nullptr)
, mbCheckedOLE(false)
, mbCheckedZip(false)
, mnLength(0)