================
@@ -99,6 +102,46 @@ void ContainerDataPointerCheck::check(const 
MatchFinder::MatchResult &Result) {
   else if (ACE)
     CE = ACE;
 
+  SourceRange ReplacementRange = UO->getSourceRange();
+  bool UseCStr = false;
+  if (CStrDecl) {
+    auto Parents = Result.Context->getParents(*UO);
+
+    if (!Parents.empty()) {
+      if (const auto *VD = Parents[0].get<VarDecl>()) {
+        QualType VarType = VD->getType();
+        if (VarType->isPointerType()) {
+          QualType PointeeType = VarType->getPointeeType();
+          UseCStr = PointeeType.isConstQualified();
+        }
+      } else if (const auto *ICE = Parents[0].get<ImplicitCastExpr>()) {
+        QualType CastType = ICE->getType();
+        if (CastType->isPointerType()) {
+          QualType PointeeType = CastType->getPointeeType();
+          UseCStr = PointeeType.isConstQualified();
+        }
+      } else if (const auto *Cast = Parents[0].get<CastExpr>()) {
+        QualType CastType = Cast->getType();
+        if (CastType->isPointerType()) {
+          QualType PointeeType = CastType->getPointeeType();
+          UseCStr = PointeeType.isConstQualified();
+          if (UseCStr) {
+            // if it's a const cast, use the Cast range as replacement range
+            // e.g. (const char*)&s[0] -> s.c_str()
+            ReplacementRange = Cast->getSourceRange();
+          }
+        }
----------------
5chmidti wrote:

You could just do all of this in the `get<CastExpr>` branch. ImplicitCastExpr 
inherits from CastExpr, so you will get the implicit one with `get<CastExpr>` 
too, and you don't have to differentiate between the, because the source range 
would either be the `UO` expression itself, or the full cast.

---

**But**, we already have 
[readability-redundant-casting](https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-casting.html),
 so we should not overload this check by having it remove the redundant cast. 
Example: https://godbolt.org/z/fG7Gsehnr

https://github.com/llvm/llvm-project/pull/190590
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to