1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 // Warn about certain redundant casts:
12 // * A reinterpret_cast<T*>(...) whose result is then implicitly cast to a void
15 // * A static_cast<T*>(e) where e is of void pointer type and whose result is
16 // then implicitly cast to a void pointer
18 // * Various const_casts that are either not needed (like casting away constness
19 // in a delete expression) or are implicitly cast back afterwards
21 // C-style casts are ignored because it makes this plugin simpler, and they
22 // should eventually be eliminated via loplugin:cstylecast and/or
23 // -Wold-style-cast. That implies that this plugin is only relevant for C++
26 #include "clang/Sema/Sema.h"
34 bool isVoidPointer(QualType type
) {
35 return type
->isPointerType()
36 && type
->getAs
<clang::PointerType
>()->getPointeeType()->isVoidType();
39 bool isRedundantConstCast(CXXConstCastExpr
const * expr
) {
40 auto const sub
= compat::getSubExprAsWritten(expr
);
42 (expr
->getType().getCanonicalType()
43 == sub
->getType().getCanonicalType())
44 && (expr
->getValueKind() != VK_XValue
45 || sub
->getValueKind() == VK_XValue
);
48 bool canConstCastFromTo(Expr
const * from
, Expr
const * to
) {
49 auto const k1
= from
->getValueKind();
50 auto const k2
= to
->getValueKind();
51 return (k2
== VK_LValue
&& k1
== VK_LValue
)
53 && (k1
!= VK_RValue
|| from
->getType()->isRecordType()));
56 char const * printExprValueKind(ExprValueKind k
) {
65 llvm_unreachable("unknown ExprValueKind");
68 enum class AlgebraicType
{ None
, Integer
, FloatingPoint
};
70 AlgebraicType
algebraicType(clang::Type
const & type
) {
71 if (type
.isIntegralOrEnumerationType()) {
72 return AlgebraicType::Integer
;
73 } else if (type
.isRealFloatingType()) {
74 return AlgebraicType::FloatingPoint
;
76 return AlgebraicType::None
;
81 public RecursiveASTVisitor
<RedundantCast
>, public loplugin::RewritePlugin
84 explicit RedundantCast(loplugin::InstantiationData
const & data
):
88 virtual void run() override
{
89 if (compiler
.getLangOpts().CPlusPlus
) {
90 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
94 bool TraverseInitListExpr(InitListExpr
* expr
, DataRecursionQueue
* queue
= nullptr) {
95 return WalkUpFromInitListExpr(expr
)
96 && TraverseSynOrSemInitListExpr(
97 expr
->isSemanticForm() ? expr
: expr
->getSemanticForm(), queue
);
100 bool VisitImplicitCastExpr(ImplicitCastExpr
const * expr
);
102 bool VisitCXXStaticCastExpr(CXXStaticCastExpr
const * expr
);
104 bool VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr
const * expr
);
106 bool VisitCXXConstCastExpr(CXXConstCastExpr
const * expr
);
108 bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr
const * expr
);
110 bool VisitCXXDynamicCastExpr(CXXDynamicCastExpr
const * expr
);
112 bool VisitCallExpr(CallExpr
const * expr
);
114 bool VisitCXXDeleteExpr(CXXDeleteExpr
const * expr
);
116 bool VisitCStyleCastExpr(CStyleCastExpr
const * expr
);
118 bool VisitBinSub(BinaryOperator
const * expr
)
119 { return visitBinOp(expr
); }
121 bool VisitBinLT(BinaryOperator
const * expr
)
122 { return visitBinOp(expr
); }
124 bool VisitBinGT(BinaryOperator
const * expr
)
125 { return visitBinOp(expr
); }
127 bool VisitBinLE(BinaryOperator
const * expr
)
128 { return visitBinOp(expr
); }
130 bool VisitBinGE(BinaryOperator
const * expr
)
131 { return visitBinOp(expr
); }
133 bool VisitBinEQ(BinaryOperator
const * expr
)
134 { return visitBinOp(expr
); }
136 bool VisitBinNE(BinaryOperator
const * expr
)
137 { return visitBinOp(expr
); }
140 bool visitBinOp(BinaryOperator
const * expr
);
141 bool isOverloadedFunction(FunctionDecl
const * decl
);
144 bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr
* expr
) {
145 if (ignoreLocation(expr
)) {
148 switch (expr
->getCastKind()) {
150 if (expr
->getType()->isPointerType()
151 || expr
->getType()->isObjectType())
153 auto e
= dyn_cast
<CXXConstCastExpr
>(
154 expr
->getSubExpr()->IgnoreParenImpCasts());
155 if (e
!= nullptr && !isRedundantConstCast(e
)) {
156 auto t1
= e
->getSubExpr()->getType().getCanonicalType();
157 auto t3
= expr
->getType().getCanonicalType();
158 bool ObjCLifetimeConversion
;
159 if (t1
.getTypePtr() == t3
.getTypePtr()
160 || (compiler
.getSema().IsQualificationConversion(
161 t1
, t3
, false, ObjCLifetimeConversion
)
162 && (e
->getType().getCanonicalType().getTypePtr()
163 != t3
.getTypePtr())))
166 DiagnosticsEngine::Warning
,
167 ("redundant const_cast from %0 to %1, result is"
168 " implicitly cast to %2"),
170 << e
->getSubExprAsWritten()->getType() << e
->getType()
171 << expr
->getType() << expr
->getSourceRange();
177 if (isVoidPointer(expr
->getType())
178 && expr
->getSubExpr()->getType()->isPointerType())
180 Expr
const * e
= expr
->getSubExpr()->IgnoreParenImpCasts();
181 while (isa
<CXXConstCastExpr
>(e
)) {
182 auto cc
= dyn_cast
<CXXConstCastExpr
>(e
);
183 if (expr
->getType()->getAs
<clang::PointerType
>()
184 ->getPointeeType().isAtLeastAsQualifiedAs(
185 cc
->getSubExpr()->getType()
186 ->getAs
<clang::PointerType
>()->getPointeeType()))
189 DiagnosticsEngine::Warning
,
190 ("redundant const_cast from %0 to %1, result is"
191 " ultimately implicitly cast to %2"),
193 << cc
->getSubExprAsWritten()->getType() << cc
->getType()
194 << expr
->getType() << expr
->getSourceRange();
196 e
= cc
->getSubExpr()->IgnoreParenImpCasts();
198 if (isa
<CXXReinterpretCastExpr
>(e
)) {
200 DiagnosticsEngine::Warning
,
201 ("redundant reinterpret_cast, result is implicitly cast to"
204 << e
->getSourceRange();
205 } else if (isa
<CXXStaticCastExpr
>(e
)
207 dyn_cast
<CXXStaticCastExpr
>(e
)->getSubExpr()
208 ->IgnoreParenImpCasts()->getType())
209 && !compiler
.getSourceManager().isMacroBodyExpansion(
213 DiagnosticsEngine::Warning
,
214 ("redundant static_cast from void pointer, result is"
215 " implicitly cast to void pointer"),
217 << e
->getSourceRange();
221 case CK_DerivedToBase
:
222 case CK_UncheckedDerivedToBase
:
223 if (expr
->getType()->isPointerType()) {
224 Expr
const * e
= expr
->getSubExpr()->IgnoreParenImpCasts();
225 while (isa
<CXXConstCastExpr
>(e
)) {
226 auto cc
= dyn_cast
<CXXConstCastExpr
>(e
);
227 if (expr
->getType()->getAs
<clang::PointerType
>()
228 ->getPointeeType().isAtLeastAsQualifiedAs(
229 cc
->getSubExpr()->getType()
230 ->getAs
<clang::PointerType
>()->getPointeeType()))
233 DiagnosticsEngine::Warning
,
234 ("redundant const_cast from %0 to %1, result is"
235 " ultimately implicitly cast to %2"),
237 << cc
->getSubExprAsWritten()->getType() << cc
->getType()
238 << expr
->getType() << expr
->getSourceRange();
240 e
= cc
->getSubExpr()->IgnoreParenImpCasts();
242 } else if (expr
->getType()->isReferenceType()) {
243 Expr
const * e
= expr
->getSubExpr()->IgnoreParenImpCasts();
244 while (isa
<CXXConstCastExpr
>(e
)) {
245 auto cc
= dyn_cast
<CXXConstCastExpr
>(e
);
246 if (expr
->getType()->getAs
<ReferenceType
>()->getPointeeType()
247 .isAtLeastAsQualifiedAs(
248 cc
->getSubExpr()->getType()
249 ->getAs
<ReferenceType
>()->getPointeeType()))
252 DiagnosticsEngine::Warning
,
253 ("redundant const_cast from %0 to %1, result is"
254 " ultimately implicitly cast to %2"),
256 << cc
->getSubExprAsWritten()->getType() << cc
->getType()
257 << expr
->getType() << expr
->getSourceRange();
259 e
= cc
->getSubExpr()->IgnoreParenImpCasts();
263 case CK_FloatingToIntegral
:
264 case CK_IntegralToFloating
:
265 if (auto e
= dyn_cast
<ExplicitCastExpr
>(expr
->getSubExpr()->IgnoreParenImpCasts())) {
266 if ((isa
<CXXStaticCastExpr
>(e
) || isa
<CXXFunctionalCastExpr
>(e
))
267 && (algebraicType(*e
->getSubExprAsWritten()->getType())
268 == algebraicType(*expr
->getType())))
271 DiagnosticsEngine::Warning
,
272 ("suspicious %select{static_cast|functional cast}0 from %1 to %2, result is"
273 " implicitly cast to %3"),
275 << isa
<CXXFunctionalCastExpr
>(e
) << e
->getSubExprAsWritten()->getType()
276 << e
->getTypeAsWritten() << expr
->getType() << expr
->getSourceRange();
286 bool RedundantCast::VisitCStyleCastExpr(CStyleCastExpr
const * expr
) {
287 if (ignoreLocation(expr
)) {
290 if (isInUnoIncludeFile(compiler
.getSourceManager().getSpellingLoc(expr
->getLocStart()))) {
293 auto t1
= compat::getSubExprAsWritten(expr
)->getType();
294 auto t2
= expr
->getTypeAsWritten();
295 if (auto templateType
= dyn_cast
<SubstTemplateTypeParmType
>(t1
)) {
296 t1
= templateType
->desugar();
301 if (!t1
->isBuiltinType() && !loplugin::TypeCheck(t1
).Enum() && !loplugin::TypeCheck(t1
).Typedef()) {
304 if (!loplugin::isOkToRemoveArithmeticCast(compiler
.getASTContext(), t1
, t2
, expr
->getSubExpr()))
308 // Ignore FD_ISSET expanding to "...(SOCKET)(fd)..." in some Microsoft
309 // winsock2.h (TODO: improve heuristic of determining that the whole
310 // expr is part of a single macro body expansion):
311 auto l1
= expr
->getLocStart();
312 while (compiler
.getSourceManager().isMacroArgExpansion(l1
)) {
313 l1
= compiler
.getSourceManager().getImmediateMacroCallerLoc(l1
);
315 auto l2
= expr
->getExprLoc();
316 while (compiler
.getSourceManager().isMacroArgExpansion(l2
)) {
317 l2
= compiler
.getSourceManager().getImmediateMacroCallerLoc(l2
);
319 auto l3
= expr
->getLocEnd();
320 while (compiler
.getSourceManager().isMacroArgExpansion(l3
)) {
321 l3
= compiler
.getSourceManager().getImmediateMacroCallerLoc(l3
);
323 if (compiler
.getSourceManager().isMacroBodyExpansion(l1
)
324 && compiler
.getSourceManager().isMacroBodyExpansion(l2
)
325 && compiler
.getSourceManager().isMacroBodyExpansion(l3
)
326 && ignoreLocation(compiler
.getSourceManager().getSpellingLoc(l2
)))
331 DiagnosticsEngine::Warning
,
332 "redundant cstyle cast from %0 to %1", expr
->getExprLoc())
333 << t1
<< t2
<< expr
->getSourceRange();
337 bool RedundantCast::VisitCXXStaticCastExpr(CXXStaticCastExpr
const * expr
) {
338 if (ignoreLocation(expr
)) {
341 auto const t2
= expr
->getTypeAsWritten();
342 bool const fnptr
= t2
->isFunctionPointerType() || t2
->isMemberFunctionPointerType();
343 auto const sub
= fnptr
? expr
->getSubExpr() : compat::getSubExprAsWritten(expr
);
344 auto const t1
= sub
->getType();
345 auto const nonClassObjectType
= t2
->isObjectType()
346 && !(t2
->isRecordType() || t2
->isArrayType());
347 if (nonClassObjectType
&& t2
.hasLocalQualifiers()) {
349 DiagnosticsEngine::Warning
,
350 ("in static_cast from %0 %1 to %2 %3, remove redundant top-level"
351 " %select{const qualifier|volatile qualifier|const volatile"
354 << t1
<< printExprValueKind(sub
->getValueKind())
355 << t2
<< printExprValueKind(expr
->getValueKind())
356 << ((t2
.isLocalConstQualified() ? 1 : 0)
357 + (t2
.isLocalVolatileQualified() ? 2 : 0) - 1)
358 << expr
->getSourceRange();
361 auto const t3
= expr
->getType();
362 auto const c1
= t1
.getCanonicalType();
363 auto const c3
= t3
.getCanonicalType();
364 if (nonClassObjectType
|| !canConstCastFromTo(sub
, expr
)
365 ? c1
.getTypePtr() != c3
.getTypePtr() : c1
!= c3
)
367 bool ObjCLifetimeConversion
;
368 if (nonClassObjectType
369 || (c1
.getTypePtr() != c3
.getTypePtr()
370 && !compiler
.getSema().IsQualificationConversion(
371 c1
, c3
, false, ObjCLifetimeConversion
)))
376 DiagnosticsEngine::Warning
,
377 "static_cast from %0 %1 to %2 %3 should be written as const_cast",
379 << t1
<< printExprValueKind(sub
->getValueKind())
380 << t2
<< printExprValueKind(expr
->getValueKind())
381 << expr
->getSourceRange();
384 if (!loplugin::isOkToRemoveArithmeticCast(compiler
.getASTContext(), t1
, t2
, expr
->getSubExpr()))
388 // Don't warn if the types are 'void *' and at least one involves a typedef
389 // (and if both involve typedefs, they're different) (this covers cases like
390 // 'oslModule', or 'CURL *', or casts between 'LPVOID' and 'HANDLE' in
391 // Windows-only code):
392 if (loplugin::TypeCheck(t1
).Pointer().NonConstVolatile().Void()) {
393 if (auto const td1
= t1
->getAs
<TypedefType
>()) {
394 auto const td2
= t2
->getAs
<TypedefType
>();
395 if (td2
== nullptr || td2
!= td1
) {
398 } else if (auto const td2
= t2
->getAs
<TypedefType
>()) {
399 auto const td1
= t1
->getAs
<TypedefType
>();
400 if (td1
== nullptr || td1
!= td2
) {
404 auto const pt1
= t1
->getAs
<clang::PointerType
>()->getPointeeType();
405 auto const pt2
= t2
->getAs
<clang::PointerType
>()->getPointeeType();
406 if (auto const ptd1
= pt1
->getAs
<TypedefType
>()) {
407 auto const ptd2
= pt2
->getAs
<TypedefType
>();
408 if (ptd2
== nullptr || ptd2
!= ptd1
) {
411 } else if (auto const ptd2
= pt2
->getAs
<TypedefType
>()) {
412 auto const ptd1
= pt1
->getAs
<TypedefType
>();
413 if (ptd1
== nullptr || ptd1
!= ptd2
) {
419 auto const k1
= sub
->getValueKind();
420 auto const k3
= expr
->getValueKind();
421 if ((k3
== VK_XValue
&& k1
!= VK_XValue
)
422 || (k3
== VK_LValue
&& k1
== VK_XValue
))
426 // Don't warn if a static_cast on a pointer to function or member function is used to
427 // disambiguate an overloaded function:
429 auto e
= sub
->IgnoreParenImpCasts();
430 if (auto const e1
= dyn_cast
<UnaryOperator
>(e
)) {
431 if (e1
->getOpcode() == UO_AddrOf
) {
432 e
= e1
->getSubExpr()->IgnoreParenImpCasts();
435 if (auto const e1
= dyn_cast
<DeclRefExpr
>(e
)) {
436 if (auto const fdecl
= dyn_cast
<FunctionDecl
>(e1
->getDecl())) {
437 if (isOverloadedFunction(fdecl
)) {
443 // Suppress warnings from static_cast<bool> in C++ definition of assert in
444 // <https://sourceware.org/git/?p=glibc.git;a=commit;
445 // h=b5889d25e9bf944a89fdd7bcabf3b6c6f6bb6f7c> "assert: Support types
446 // without operator== (int) [BZ #21972]":
447 if (t1
->isBooleanType() && t2
->isBooleanType()) {
448 auto loc
= expr
->getLocStart();
449 if (compiler
.getSourceManager().isMacroBodyExpansion(loc
)
450 && (Lexer::getImmediateMacroName(
451 loc
, compiler
.getSourceManager(), compiler
.getLangOpts())
458 DiagnosticsEngine::Warning
,
459 ("static_cast from %0 %1 to %2 %3 is redundant%select{| or should be"
460 " written as an explicit construction of a temporary}4"),
462 << t1
<< printExprValueKind(k1
) << t2
<< printExprValueKind(k3
)
463 << (k3
== VK_RValue
&& (k1
!= VK_RValue
|| t1
->isRecordType()))
464 << expr
->getSourceRange();
468 bool RedundantCast::VisitCXXReinterpretCastExpr(
469 CXXReinterpretCastExpr
const * expr
)
471 if (ignoreLocation(expr
)) {
474 if (expr
->getSubExpr()->getType()->isVoidPointerType()) {
475 auto t
= expr
->getType()->getAs
<clang::PointerType
>();
476 if (t
== nullptr || !t
->getPointeeType()->isObjectType()) {
479 if (rewriter
!= nullptr) {
480 auto loc
= expr
->getLocStart();
481 while (compiler
.getSourceManager().isMacroArgExpansion(loc
)) {
482 loc
= compiler
.getSourceManager().getImmediateMacroCallerLoc(
485 if (compiler
.getSourceManager().isMacroBodyExpansion(loc
)) {
486 auto loc2
= expr
->getLocEnd();
487 while (compiler
.getSourceManager().isMacroArgExpansion(loc2
)) {
488 loc2
= compiler
.getSourceManager()
489 .getImmediateMacroCallerLoc(loc2
);
491 if (compiler
.getSourceManager().isMacroBodyExpansion(loc2
)) {
492 //TODO: check loc, loc2 are in same macro body expansion
493 loc
= compiler
.getSourceManager().getSpellingLoc(loc
);
496 auto s
= compiler
.getSourceManager().getCharacterData(loc
);
497 auto n
= Lexer::MeasureTokenLength(
498 loc
, compiler
.getSourceManager(), compiler
.getLangOpts());
499 std::string
tok(s
, n
);
500 if (tok
== "reinterpret_cast" && replaceText(loc
, n
, "static_cast"))
506 DiagnosticsEngine::Warning
,
507 "reinterpret_cast from %0 to %1 can be simplified to static_cast",
509 << expr
->getSubExprAsWritten()->getType() << expr
->getType()
510 << expr
->getSourceRange();
511 } else if (expr
->getType()->isVoidPointerType()) {
512 auto t
= expr
->getSubExpr()->getType()->getAs
<clang::PointerType
>();
513 if (t
== nullptr || !t
->getPointeeType()->isObjectType()) {
517 DiagnosticsEngine::Warning
,
518 ("reinterpret_cast from %0 to %1 can be simplified to static_cast"
519 " or an implicit conversion"),
521 << expr
->getSubExprAsWritten()->getType() << expr
->getType()
522 << expr
->getSourceRange();
523 } else if (expr
->getType()->isFundamentalType()) {
524 if (auto const sub
= dyn_cast
<CXXConstCastExpr
>(
525 expr
->getSubExpr()->IgnoreParens()))
528 DiagnosticsEngine::Warning
,
529 ("redundant const_cast from %0 to %1 within reinterpret_cast to"
530 " fundamental type %2"),
532 << sub
->getSubExprAsWritten()->getType()
533 << sub
->getTypeAsWritten() << expr
->getTypeAsWritten()
534 << expr
->getSourceRange();
541 bool RedundantCast::VisitCXXConstCastExpr(CXXConstCastExpr
const * expr
) {
542 if (ignoreLocation(expr
)) {
545 auto const sub
= compat::getSubExprAsWritten(expr
);
546 if (isRedundantConstCast(expr
)) {
548 DiagnosticsEngine::Warning
,
549 "redundant const_cast from %0 %1 to %2 %3", expr
->getExprLoc())
550 << sub
->getType() << printExprValueKind(sub
->getValueKind())
551 << expr
->getTypeAsWritten()
552 << printExprValueKind(expr
->getValueKind())
553 << expr
->getSourceRange();
556 if (auto const dce
= dyn_cast
<CXXStaticCastExpr
>(
557 sub
->IgnoreParenImpCasts()))
559 auto const sub2
= compat::getSubExprAsWritten(dce
);
560 auto t1
= sub2
->getType().getCanonicalType();
561 auto isNullptr
= t1
->isNullPtrType();
562 auto t2
= dce
->getType().getCanonicalType();
563 auto t3
= expr
->getType().getCanonicalType();
564 auto redundant
= false;
566 if ((t2
.isConstQualified()
567 && (isNullptr
|| !t1
.isConstQualified())
568 && !t3
.isConstQualified())
569 || (t2
.isVolatileQualified()
570 && (isNullptr
|| !t1
.isVolatileQualified())
571 && !t3
.isVolatileQualified()))
577 auto const p1
= t1
->getAs
<clang::PointerType
>();
581 t1
= p1
->getPointeeType();
582 isNullptr
= t1
->isNullPtrType();
584 auto const p2
= t2
->getAs
<clang::PointerType
>();
588 t2
= p2
->getPointeeType();
589 auto const p3
= t3
->getAs
<clang::PointerType
>();
593 t3
= p3
->getPointeeType();
597 DiagnosticsEngine::Warning
,
598 ("redundant static_cast/const_cast combination from %0 via %1"
601 << sub2
->getType() << dce
->getTypeAsWritten()
602 << expr
->getTypeAsWritten() << expr
->getSourceRange();
608 bool RedundantCast::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr
const * expr
) {
609 if (ignoreLocation(expr
)) {
612 // Restrict this to "real" casts (compared to uses of braced-init-list, like
618 // std::initializer_list<Foo>{bar, baz}
620 // ), and only to cases where the sub-expression already is a prvalue of
621 // non-class type (and thus the cast is unlikely to be meant to create a
623 auto const t1
= expr
->getTypeAsWritten();
624 bool const fnptr
= t1
->isFunctionPointerType() || t1
->isMemberFunctionPointerType();
625 auto const sub
= fnptr
? expr
->getSubExpr() : compat::getSubExprAsWritten(expr
);
626 if (sub
->getValueKind() != VK_RValue
|| expr
->getType()->isRecordType()
627 || isa
<InitListExpr
>(sub
) || isa
<CXXStdInitializerListExpr
>(sub
))
632 // See "There might even be good reasons(?) not to warn inside explicit
633 // casts" block in compilerplugins/clang/test/cppunitassertequals.cxx:
634 auto const eloc
= expr
->getExprLoc();
635 if (compiler
.getSourceManager().isMacroArgExpansion(eloc
)) {
636 auto const name
= Lexer::getImmediateMacroName(
637 eloc
, compiler
.getSourceManager(), compiler
.getLangOpts());
638 if (name
== "CPPUNIT_ASSERT" || name
== "CPPUNIT_ASSERT_MESSAGE") {
643 // Don't warn if a functional cast on a pointer to function or member function is used to
644 // disambiguate an overloaded function:
646 auto e
= sub
->IgnoreParenImpCasts();
647 if (auto const e1
= dyn_cast
<UnaryOperator
>(e
)) {
648 if (e1
->getOpcode() == UO_AddrOf
) {
649 e
= e1
->getSubExpr()->IgnoreParenImpCasts();
652 if (auto const e1
= dyn_cast
<DeclRefExpr
>(e
)) {
653 if (auto const fdecl
= dyn_cast
<FunctionDecl
>(e1
->getDecl())) {
654 if (isOverloadedFunction(fdecl
)) {
661 // See the commit message of d0e7d020fa405ab94f19916ec96fbd4611da0031
662 // "socket.c -> socket.cxx" for the reason to have
664 // bool(FD_ISSET(...))
666 // in sal/osl/unx/socket.cxx:
667 //TODO: Better check that sub is exactly an expansion of FD_ISSET:
668 if (sub
->getLocEnd().isMacroID()) {
669 for (auto loc
= sub
->getLocStart();
671 && (compiler
.getSourceManager()
672 .isAtStartOfImmediateMacroExpansion(loc
));
673 loc
= compiler
.getSourceManager().getImmediateMacroCallerLoc(loc
))
675 if (Lexer::getImmediateMacroName(
676 loc
, compiler
.getSourceManager(), compiler
.getLangOpts())
684 auto const t2
= sub
->getType();
685 if (t1
.getCanonicalType() != t2
.getCanonicalType())
687 if (!loplugin::isOkToRemoveArithmeticCast(compiler
.getASTContext(), t1
, t2
, expr
->getSubExpr()))
690 DiagnosticsEngine::Warning
,
691 "redundant functional cast from %0 to %1", expr
->getExprLoc())
692 << t2
<< t1
<< expr
->getSourceRange();
696 bool RedundantCast::VisitCXXDynamicCastExpr(CXXDynamicCastExpr
const * expr
) {
697 if (ignoreLocation(expr
)) {
700 // so far this only deals with dynamic casting from T to T
701 auto const sub
= compat::getSubExprAsWritten(expr
);
702 auto const t1
= expr
->getTypeAsWritten();
703 auto const t2
= sub
->getType();
704 if (t1
.getCanonicalType() != t2
.getCanonicalType())
707 DiagnosticsEngine::Warning
,
708 "redundant dynamic cast from %0 to %1", expr
->getExprLoc())
709 << t2
<< t1
<< expr
->getSourceRange();
713 bool RedundantCast::VisitCallExpr(CallExpr
const * expr
) {
714 if (ignoreLocation(expr
)) {
717 auto f
= expr
->getDirectCallee();
718 if (f
== nullptr || !f
->isVariadic()
719 || expr
->getNumArgs() <= f
->getNumParams())
723 for (auto i
= f
->getNumParams(); i
!= expr
->getNumArgs(); ++i
) {
724 auto a
= expr
->getArg(i
);
725 if (a
->getType()->isPointerType()) {
726 auto e
= dyn_cast
<CXXConstCastExpr
>(a
->IgnoreParenImpCasts());
729 DiagnosticsEngine::Warning
,
730 "redundant const_cast of variadic function argument",
732 << expr
->getSourceRange();
739 bool RedundantCast::VisitCXXDeleteExpr(CXXDeleteExpr
const * expr
) {
740 if (ignoreLocation(expr
)) {
743 auto e
= dyn_cast
<CXXConstCastExpr
>(
744 expr
->getArgument()->IgnoreParenImpCasts());
747 DiagnosticsEngine::Warning
,
748 "redundant const_cast in delete expression", e
->getExprLoc())
749 << expr
->getSourceRange();
754 bool RedundantCast::visitBinOp(BinaryOperator
const * expr
) {
755 if (ignoreLocation(expr
)) {
758 if (expr
->getLHS()->getType()->isPointerType()
759 && expr
->getRHS()->getType()->isPointerType())
761 auto e
= dyn_cast
<CXXConstCastExpr
>(
762 expr
->getLHS()->IgnoreParenImpCasts());
765 DiagnosticsEngine::Warning
,
766 "redundant const_cast on lhs of pointer %select{comparison|subtraction}0 expression",
768 << (expr
->getOpcode() == BO_Sub
) << expr
->getSourceRange();
770 e
= dyn_cast
<CXXConstCastExpr
>(
771 expr
->getRHS()->IgnoreParenImpCasts());
774 DiagnosticsEngine::Warning
,
775 "redundant const_cast on rhs of pointer %select{comparison|subtraction}0 expression",
777 << (expr
->getOpcode() == BO_Sub
) << expr
->getSourceRange();
783 bool RedundantCast::isOverloadedFunction(FunctionDecl
const * decl
) {
784 auto const ctx
= decl
->getDeclContext();
785 if (!ctx
->isLookupContext()) {
788 auto const canon
= decl
->getCanonicalDecl();
789 auto const res
= ctx
->lookup(decl
->getDeclName());
790 for (auto d
= res
.begin(); d
!= res
.end(); ++d
) {
791 if (auto const f
= dyn_cast
<FunctionDecl
>(*d
)) {
792 if (f
->getCanonicalDecl() != canon
) {
800 loplugin::Plugin::Registration
<RedundantCast
> X("redundantcast", true);
804 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */