[clang-tools-extra] [clang-tidy] Add readability-redundant-casting check (PR #70595)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 10:13:58 PST 2024


================
@@ -0,0 +1,242 @@
+//===--- 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 areTypesEquals(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 areTypesEquals(PtrS, PtrD);
+
+  const DeducedType *DT = S->getContainedDeducedType();
+  if (DT && DT->isDeduced())
+    return D == DT->getDeducedType();
+
+  return false;
+}
+
+static bool areTypesEquals(QualType TypeS, QualType TypeD,
+                           bool IgnoreTypeAliases) {
+  const QualType CTypeS = TypeS.getCanonicalType();
+  const QualType CTypeD = TypeD.getCanonicalType();
+  if (CTypeS != CTypeD)
+    return false;
+
+  return IgnoreTypeAliases || areTypesEquals(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 NonReferenceType =
+        WithoutImplicitAndParen->getType().getNonReferenceType();
+    if (!areTypesEquals(
+            B->getLHS()->IgnoreImplicit()->getType().getNonReferenceType(),
+            NonReferenceType, IgnoreTypeAliases))
+      return true;
+    if (!areTypesEquals(
+            B->getRHS()->IgnoreImplicit()->getType().getNonReferenceType(),
+            NonReferenceType, IgnoreTypeAliases))
----------------
PiotrZSL wrote:

Done

https://github.com/llvm/llvm-project/pull/70595


More information about the cfe-commits mailing list