11 #include "../utils/Matchers.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Lex/Lexer.h" 22 namespace readability {
24 void RedundantMemberInitCheck::registerMatchers(MatchFinder *Finder) {
25 if (!getLangOpts().CPlusPlus)
30 hasDeclaration(cxxConstructorDecl(hasParent(
36 unless(isDelegatingConstructor()),
38 anyOf(isUnion(), ast_matchers::isTemplateInstantiation()))),
39 forEachConstructorInitializer(
40 cxxCtorInitializer(isWritten(),
41 withInitializer(ignoringImplicit(Construct)),
42 unless(forField(hasType(isConstQualified()))),
43 unless(forField(hasParent(recordDecl(isUnion())))))
48 void RedundantMemberInitCheck::check(
const MatchFinder::MatchResult &
Result) {
49 const auto *Init = Result.Nodes.getNodeAs<CXXCtorInitializer>(
"init");
50 const auto *Construct = Result.Nodes.getNodeAs<CXXConstructExpr>(
"construct");
52 if (Construct->getNumArgs() == 0 ||
53 Construct->getArg(0)->isDefaultArgument()) {
54 if (Init->isAnyMemberInitializer()) {
55 diag(Init->getSourceLocation(),
"initializer for member %0 is redundant")
56 << Init->getAnyMember()
57 << FixItHint::CreateRemoval(Init->getSourceRange());
59 diag(Init->getSourceLocation(),
60 "initializer for base class %0 is redundant")
61 << Construct->getType()
62 << FixItHint::CreateRemoval(Init->getSourceRange());
bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context)
Returns true if Type is trivially default constructible.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//