11 #include "../utils/Matchers.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Lex/Lexer.h"
17 using namespace clang::ast_matchers;
18 using namespace clang::tidy::matchers;
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()))))
47 void RedundantMemberInitCheck::check(
const MatchFinder::MatchResult &
Result) {
48 const auto *Init = Result.Nodes.getNodeAs<CXXCtorInitializer>(
"init");
49 const auto *Construct = Result.Nodes.getNodeAs<CXXConstructExpr>(
"construct");
51 if (Construct->getNumArgs() == 0 ||
52 Construct->getArg(0)->isDefaultArgument()) {
53 if (Init->isAnyMemberInitializer()) {
54 diag(Init->getSourceLocation(),
"initializer for member %0 is redundant")
56 << FixItHint::CreateRemoval(Init->getSourceRange());
58 diag(Init->getSourceLocation(),
59 "initializer for base class %0 is redundant")
60 << Construct->getType()
61 << FixItHint::CreateRemoval(Init->getSourceRange());
std::unique_ptr< ast_matchers::MatchFinder > Finder
bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context)
Returns true if Type is trivially default constructible.