[clang-tools-extra] [clang-tidy] Add readability-redundant-casting check (PR #70595)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 18 11:05:03 PST 2024
================
@@ -0,0 +1,254 @@
+//===--- RedundantCastingCheck.cpp - clang-tidy ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RedundantCastingCheck.h"
+#include "../utils/FixItHintUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+static bool areTypesEqual(QualType S, QualType D) {
+ if (S == D)
+ return true;
+
+ const auto *TS = S->getAs<TypedefType>();
+ const auto *TD = D->getAs<TypedefType>();
+ if (TS != TD)
+ return false;
+
+ QualType PtrS = S->getPointeeType();
+ QualType PtrD = D->getPointeeType();
+
+ if (!PtrS.isNull() && !PtrD.isNull())
+ return areTypesEqual(PtrS, PtrD);
+
+ const DeducedType *DT = S->getContainedDeducedType();
+ if (DT && DT->isDeduced())
+ return D == DT->getDeducedType();
+
+ return false;
+}
+
+static bool areTypesEqual(QualType TypeS, QualType TypeD,
+ bool IgnoreTypeAliases) {
+ const QualType CTypeS = TypeS.getCanonicalType();
+ const QualType CTypeD = TypeD.getCanonicalType();
+ if (CTypeS != CTypeD)
+ return false;
+
+ return IgnoreTypeAliases || areTypesEqual(TypeS.getLocalUnqualifiedType(),
+ TypeD.getLocalUnqualifiedType());
+}
+
+static bool areBinaryOperatorOperandsTypesEqual(const Expr *E,
+ bool IgnoreTypeAliases) {
+ if (!E)
+ return false;
+ const Expr *WithoutImplicitAndParen = E->IgnoreParenImpCasts();
+ if (!WithoutImplicitAndParen)
+ return false;
+ if (const auto *B = dyn_cast<BinaryOperator>(WithoutImplicitAndParen)) {
+ const QualType Type = WithoutImplicitAndParen->getType();
+ if (Type.isNull())
+ return false;
+
+ const QualType NonReferenceType = Type.getNonReferenceType();
+ const QualType LHSType = B->getLHS()->IgnoreImplicit()->getType();
+ if (!LHSType.isNull() &&
+ !areTypesEqual(LHSType.getNonReferenceType(), NonReferenceType,
+ IgnoreTypeAliases))
+ return true;
+ const QualType RHSType = B->getRHS()->IgnoreImplicit()->getType();
+ if (!RHSType.isNull() &&
+ !areTypesEqual(RHSType.getNonReferenceType(), NonReferenceType,
+ IgnoreTypeAliases))
+ return true;
+ }
+ return false;
+}
----------------
PiotrZSL wrote:
Agree
https://github.com/llvm/llvm-project/pull/70595
More information about the cfe-commits
mailing list