11#include "clang/AST/ASTContext.h"
12#include "clang/AST/TypeBase.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
24 const auto *TS = S->getAs<TypedefType>();
25 const auto *TD = D->getAs<TypedefType>();
29 const QualType PtrS = S->getPointeeType();
30 const QualType PtrD = D->getPointeeType();
32 if (!PtrS.isNull() && !PtrD.isNull())
33 return areTypesEqual(PtrS.IgnoreParens(), PtrD.IgnoreParens());
35 const DeducedType *DT = S->getContainedDeducedType();
36 if (DT && DT->isDeduced())
37 return D == DT->getDeducedType();
43 bool IgnoreTypeAliases) {
44 const QualType CTypeS = TypeS.getCanonicalType();
45 const QualType CTypeD = TypeD.getCanonicalType();
49 return IgnoreTypeAliases ||
areTypesEqual(TypeS.getLocalUnqualifiedType(),
50 TypeD.getLocalUnqualifiedType());
54 const Expr *E,
bool IgnoreTypeAliases,
bool IgnoreImplicitCasts) {
57 const Expr *WithoutImplicitAndParen = E->IgnoreParenImpCasts();
58 if (!WithoutImplicitAndParen)
60 if (
const auto *B = dyn_cast<BinaryOperator>(WithoutImplicitAndParen)) {
61 const QualType Type = WithoutImplicitAndParen->getType();
65 const QualType NonReferenceType = Type.getNonReferenceType();
66 const QualType LHSType = B->getLHS()->IgnoreImplicit()->getType();
67 const QualType RHSType = B->getRHS()->IgnoreImplicit()->getType();
68 const bool LHSMatches =
69 !LHSType.isNull() &&
areTypesEqual(LHSType.getNonReferenceType(),
70 NonReferenceType, IgnoreTypeAliases);
71 const bool RHSMatches =
72 !RHSType.isNull() &&
areTypesEqual(RHSType.getNonReferenceType(),
73 NonReferenceType, IgnoreTypeAliases);
78 const bool CastIsNeeded = IgnoreImplicitCasts
79 ? (!LHSMatches || !RHSMatches)
80 : (!LHSMatches && !RHSMatches);
88 const Expr *CleanSourceExpr = SourceExpr->IgnoreParenImpCasts();
89 if (
const auto *E = dyn_cast<DeclRefExpr>(CleanSourceExpr))
92 if (
const auto *E = dyn_cast<CallExpr>(CleanSourceExpr))
93 return E->getCalleeDecl();
95 if (
const auto *E = dyn_cast<MemberExpr>(CleanSourceExpr))
96 return E->getMemberDecl();
103 IgnoreMacros(Options.get(
"IgnoreMacros", true)),
104 IgnoreTypeAliases(Options.get(
"IgnoreTypeAliases", false)),
105 IgnoreImplicitCasts(Options.get(
"IgnoreImplicitCasts", false)) {}
108 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
109 Options.store(Opts,
"IgnoreTypeAliases", IgnoreTypeAliases);
110 Options.store(Opts,
"IgnoreImplicitCasts", IgnoreImplicitCasts);
114 auto SimpleType = qualType(hasCanonicalType(
115 qualType(anyOf(builtinType(), references(builtinType()),
116 references(pointsTo(qualType())), pointsTo(qualType())))));
118 auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField())));
120 const ast_matchers::internal::VariadicDynCastAllOfMatcher<
121 Stmt, CXXParenListInitExpr>
122 cxxParenListInitExpr;
126 unless(hasCastKind(CK_ConstructorConversion)),
127 unless(hasCastKind(CK_UserDefinedConversion)),
128 unless(cxxFunctionalCastExpr(hasDestinationType(unless(SimpleType)))),
130 hasDestinationType(qualType().bind(
"dstType")),
131 hasSourceExpression(anyOf(
132 expr(unless(initListExpr()), unless(BitfieldMemberExpr),
133 unless(cxxParenListInitExpr()),
134 hasType(qualType().bind(
"srcType")))
136 initListExpr(unless(hasInit(1, expr())),
137 hasInit(0, expr(unless(BitfieldMemberExpr),
138 hasType(qualType().bind(
"srcType")))
145 const auto *SourceExpr = Result.Nodes.getNodeAs<Expr>(
"source");
146 auto TypeD = *Result.Nodes.getNodeAs<QualType>(
"dstType");
148 if (SourceExpr->getValueKind() == VK_LValue &&
149 TypeD.getCanonicalType()->isRValueReferenceType())
153 Result.Nodes.getNodeAs<QualType>(
"srcType")->getNonReferenceType();
154 TypeD = TypeD.getNonReferenceType();
160 SourceExpr, IgnoreTypeAliases, IgnoreImplicitCasts))
163 const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>(
"cast");
165 (CastExpr->getBeginLoc().isMacroID() ||
166 CastExpr->getEndLoc().isMacroID() || CastExpr->getExprLoc().isMacroID()))
171 diag(CastExpr->getExprLoc(),
172 "redundant explicit casting to the same type %0 as the "
173 "sub-expression, remove this casting");
176 const SourceManager &SM = *Result.SourceManager;
177 const SourceLocation SourceExprBegin =
178 SM.getExpansionLoc(SourceExpr->getBeginLoc());
179 const SourceLocation SourceExprEnd =
180 SM.getExpansionLoc(SourceExpr->getEndLoc());
182 if (SourceExprBegin != CastExpr->getBeginLoc())
183 Diag << FixItHint::CreateRemoval(SourceRange(
184 CastExpr->getBeginLoc(), SourceExprBegin.getLocWithOffset(-1)));
186 const SourceLocation NextToken = Lexer::getLocForEndOfToken(
187 SourceExprEnd, 0U, SM, Result.Context->getLangOpts());
189 if (SourceExprEnd != CastExpr->getEndLoc()) {
190 Diag << FixItHint::CreateRemoval(
191 SourceRange(NextToken, CastExpr->getEndLoc()));
195 Diag << FixItHint::CreateInsertion(SourceExprBegin,
"(")
196 << FixItHint::CreateInsertion(NextToken,
")");
204 if (
const auto *D = dyn_cast<CXXConstructorDecl>(SourceExprDecl)) {
205 diag(
D->getLocation(),
206 "source type originates from the invocation of this constructor",
207 DiagnosticIDs::Note);
211 if (
const auto *D = dyn_cast<FunctionDecl>(SourceExprDecl)) {
212 diag(
D->getLocation(),
213 "source type originates from the invocation of this "
214 "%select{function|method}0",
216 << isa<CXXMethodDecl>(D) <<
D->getReturnTypeSourceRange();
220 if (
const auto *D = dyn_cast<FieldDecl>(SourceExprDecl)) {
221 diag(
D->getLocation(),
222 "source type originates from referencing this member",
224 << SourceRange(
D->getTypeSpecStartLoc(),
D->getTypeSpecEndLoc());
228 if (
const auto *D = dyn_cast<ParmVarDecl>(SourceExprDecl)) {
229 diag(
D->getLocation(),
230 "source type originates from referencing this parameter",
232 << SourceRange(
D->getTypeSpecStartLoc(),
D->getTypeSpecEndLoc());
236 if (
const auto *D = dyn_cast<VarDecl>(SourceExprDecl)) {
237 diag(
D->getLocation(),
238 "source type originates from referencing this variable",
240 << SourceRange(
D->getTypeSpecStartLoc(),
D->getTypeSpecEndLoc());
244 if (
const auto *D = dyn_cast<EnumConstantDecl>(SourceExprDecl)) {
245 diag(
D->getLocation(),
246 "source type originates from referencing this enum constant",
247 DiagnosticIDs::Note);
251 if (
const auto *D = dyn_cast<BindingDecl>(SourceExprDecl)) {
252 diag(
D->getLocation(),
253 "source type originates from referencing this bound variable",
254 DiagnosticIDs::Note);
258 if (
const auto *D = dyn_cast<NonTypeTemplateParmDecl>(SourceExprDecl)) {
259 diag(
D->getLocation(),
260 "source type originates from referencing this non-type template "
262 DiagnosticIDs::Note);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
RedundantCastingCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
static bool areTypesEqual(QualType S, QualType D)
static const Decl * getSourceExprDecl(const Expr *SourceExpr)
static bool binaryOperatorOperandsTypesEqualToOperatorResultType(const Expr *E, bool IgnoreTypeAliases, bool IgnoreImplicitCasts)
bool areParensNeededForStatement(const Stmt &Node)
llvm::StringMap< ClangTidyValue > OptionMap