Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 1 | //===--- RedundantMemberInitCheck.cpp - clang-tidy-------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "RedundantMemberInitCheck.h" |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 11 | #include "../utils/Matchers.h" |
Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | #include "clang/Lex/Lexer.h" |
Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | |
| 17 | using namespace clang::ast_matchers; |
| 18 | using namespace clang::tidy::matchers; |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | namespace readability { |
| 23 | |
| 24 | void RedundantMemberInitCheck::registerMatchers(MatchFinder *Finder) { |
Malcolm Parsons | 51bfe42 | 2016-12-27 22:14:40 +0000 | [diff] [blame] | 25 | if (!getLangOpts().CPlusPlus) |
| 26 | return; |
| 27 | |
Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 28 | auto Construct = |
| 29 | cxxConstructExpr( |
| 30 | hasDeclaration(cxxConstructorDecl(hasParent( |
| 31 | cxxRecordDecl(unless(isTriviallyDefaultConstructible())))))) |
| 32 | .bind("construct"); |
| 33 | |
| 34 | Finder->addMatcher( |
| 35 | cxxConstructorDecl( |
| 36 | unless(isDelegatingConstructor()), |
| 37 | ofClass(unless( |
| 38 | anyOf(isUnion(), ast_matchers::isTemplateInstantiation()))), |
| 39 | forEachConstructorInitializer( |
| 40 | cxxCtorInitializer(isWritten(), |
| 41 | withInitializer(ignoringImplicit(Construct)), |
Malcolm Parsons | 2f968433 | 2017-08-01 09:54:05 +0000 | [diff] [blame] | 42 | unless(forField(hasType(isConstQualified()))), |
| 43 | unless(forField(hasParent(recordDecl(isUnion()))))) |
Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 44 | .bind("init"))), |
| 45 | this); |
| 46 | } |
| 47 | |
| 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"); |
| 51 | |
| 52 | if (Construct->getNumArgs() == 0 || |
| 53 | Construct->getArg(0)->isDefaultArgument()) { |
| 54 | if (Init->isAnyMemberInitializer()) { |
| 55 | diag(Init->getSourceLocation(), "initializer for member %0 is redundant") |
Malcolm Parsons | 2f968433 | 2017-08-01 09:54:05 +0000 | [diff] [blame] | 56 | << Init->getAnyMember() |
Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 57 | << FixItHint::CreateRemoval(Init->getSourceRange()); |
| 58 | } else { |
| 59 | diag(Init->getSourceLocation(), |
| 60 | "initializer for base class %0 is redundant") |
Malcolm Parsons | 49fe403 | 2016-11-15 17:49:00 +0000 | [diff] [blame] | 61 | << Construct->getType() |
Malcolm Parsons | 5c24a11 | 2016-10-20 16:08:03 +0000 | [diff] [blame] | 62 | << FixItHint::CreateRemoval(Init->getSourceRange()); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | } // namespace readability |
| 68 | } // namespace tidy |
| 69 | } // namespace clang |