njames93 created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76761

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
===================================================================
--- 
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
+++ 
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
@@ -220,3 +220,16 @@
   m1tp m1p2 = m1;
   m1p2(s.c_str());  
 }
+
+namespace PR45286 {
+struct Foo {
+  void func(const std::string &) {}
+};
+
+void bar() {
+  std::string Str{"aaa"};
+  Foo Foo;
+  Foo.func(Str.c_str());
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant call to 'c_str' 
[readability-redundant-string-cstr]
+}
+} // namespace PR45286
Index: clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
@@ -83,24 +83,25 @@
 // Check that ParamDecl of CallExprDecl has rvalue type.
 static bool checkParamDeclOfAncestorCallExprHasRValueRefType(
     const Expr *TheCxxConstructExpr, ASTContext &Context) {
-  if (const clang::CallExpr *TheCallExpr =
-          tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr,
-                                                    Context)) {
-    for (unsigned i = 0; i < TheCallExpr->getNumArgs(); ++i) {
-      const Expr *Arg = TheCallExpr->getArg(i);
-      if (Arg->getSourceRange() == TheCxxConstructExpr->getSourceRange()) {
-        if (const auto *TheCallExprFuncProto =
-                TheCallExpr->getCallee()
-                    ->getType()
-                    ->getPointeeType()
-                    ->getAs<FunctionProtoType>()) {
-          if (TheCallExprFuncProto->getParamType(i)->isRValueReferenceType())
-            return true;
-        }
-      }
+  const clang::CallExpr *TheCallExpr =
+      tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr, Context);
+  if (!TheCallExpr)
+    return false;
+  for (unsigned I = 0; I < TheCallExpr->getNumArgs(); ++I) {
+    const Expr *Arg = TheCallExpr->getArg(I);
+    if (Arg->getSourceRange() != TheCxxConstructExpr->getSourceRange())
+      continue;
+    QualType CalleePointerType =
+        TheCallExpr->getCallee()->getType()->getPointeeType();
+    if (CalleePointerType.isNull())
+      // Not a pointer type, should probably avoid this
+      continue;
+    if (const auto *TheCallExprFuncProto =
+            CalleePointerType->getAs<FunctionProtoType>()) {
+      if (TheCallExprFuncProto->getParamType(I)->isRValueReferenceType())
+        return true;
     }
   }
-
   return false;
 }
 


Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp
@@ -220,3 +220,16 @@
   m1tp m1p2 = m1;
   m1p2(s.c_str());  
 }
+
+namespace PR45286 {
+struct Foo {
+  void func(const std::string &) {}
+};
+
+void bar() {
+  std::string Str{"aaa"};
+  Foo Foo;
+  Foo.func(Str.c_str());
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+}
+} // namespace PR45286
Index: clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
@@ -83,24 +83,25 @@
 // Check that ParamDecl of CallExprDecl has rvalue type.
 static bool checkParamDeclOfAncestorCallExprHasRValueRefType(
     const Expr *TheCxxConstructExpr, ASTContext &Context) {
-  if (const clang::CallExpr *TheCallExpr =
-          tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr,
-                                                    Context)) {
-    for (unsigned i = 0; i < TheCallExpr->getNumArgs(); ++i) {
-      const Expr *Arg = TheCallExpr->getArg(i);
-      if (Arg->getSourceRange() == TheCxxConstructExpr->getSourceRange()) {
-        if (const auto *TheCallExprFuncProto =
-                TheCallExpr->getCallee()
-                    ->getType()
-                    ->getPointeeType()
-                    ->getAs<FunctionProtoType>()) {
-          if (TheCallExprFuncProto->getParamType(i)->isRValueReferenceType())
-            return true;
-        }
-      }
+  const clang::CallExpr *TheCallExpr =
+      tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr, Context);
+  if (!TheCallExpr)
+    return false;
+  for (unsigned I = 0; I < TheCallExpr->getNumArgs(); ++I) {
+    const Expr *Arg = TheCallExpr->getArg(I);
+    if (Arg->getSourceRange() != TheCxxConstructExpr->getSourceRange())
+      continue;
+    QualType CalleePointerType =
+        TheCallExpr->getCallee()->getType()->getPointeeType();
+    if (CalleePointerType.isNull())
+      // Not a pointer type, should probably avoid this
+      continue;
+    if (const auto *TheCallExprFuncProto =
+            CalleePointerType->getAs<FunctionProtoType>()) {
+      if (TheCallExprFuncProto->getParamType(I)->isRValueReferenceType())
+        return true;
     }
   }
-
   return false;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to