/* -*- 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/.
*/
// ignore some contexts within which nullptr is fine bool TraverseReturnStmt(ReturnStmt*) { returntrue; } bool TraverseInitListExpr(InitListExpr*) { returntrue; } bool TraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr*) { returntrue; } // ignore them for the shared visitor too bool PreTraverseReturnStmt(ReturnStmt*) { returnfalse; } bool PreTraverseInitListExpr(InitListExpr*) { returnfalse; } bool PreTraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr*) { returnfalse; }
};
bool SimplifyConstruct::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
{ if (ignoreLocation(constructExpr)) returntrue; 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()) returntrue; if (constructExpr->getNumArgs() == 1
&& isa<CXXNullPtrLiteralExpr>(constructExpr->getArg(0)->IgnoreParenImpCasts()))
{
report(DiagnosticsEngine::Warning, "no need to explicitly init an instance of %0 with nullptr, just use default " "constructor",
constructExpr->getSourceRange().getBegin())
<< constructExpr->getType() << constructExpr->getSourceRange();
} returntrue;
}
bool SimplifyConstruct::VisitVarDecl(VarDecl const* varDecl)
{ if (ignoreLocation(varDecl)) returntrue; // cannot use OUString s("xxx") style syntax in a parameter if (isa<ParmVarDecl>(varDecl)) returntrue;
varDecl = varDecl->getCanonicalDecl(); if (!varDecl->getInit()) returntrue; if (varDecl->getInitStyle() != VarDecl::InitializationStyle::CInit) returntrue; if (!varDecl->getType()->isRecordType()) returntrue; if (isa<AutoType>(varDecl->getType())) returntrue;
auto init = varDecl->getInit(); autoconst e1 = init->IgnoreImplicit(); if (!isa<CXXFunctionalCastExpr>(e1) && !isa<CXXTemporaryObjectExpr>(e1)) returntrue;
// e.g. the LANGUAGE_DONTKNOW defines if (compiler.getSourceManager().isMacroBodyExpansion(init->getBeginLoc())) returntrue;
report(DiagnosticsEngine::Warning, "simplify construction, just use 'Foo a(...);'",
varDecl->getLocation())
<< varDecl->getSourceRange();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.