[PATCH] D18829: [clang-tidy] Fix FP with readability-redundant-string-init for default arguments

Etienne Bergeron via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 6 08:38:21 PDT 2016


etienneb created this revision.
etienneb added a reviewer: alexfh.
etienneb added a subscriber: cfe-commits.

Clang-tidy is reporting a warning of redundant string initialisation
on a string parameter initialized with empty string.

See bug: 27087

The reported example is:
```
#include <string>
void fn(std::string a = "");
```

http://reviews.llvm.org/D18829

Files:
  clang-tidy/readability/RedundantStringInitCheck.cpp
  test/clang-tidy/readability-redundant-string-init.cpp

Index: test/clang-tidy/readability-redundant-string-init.cpp
===================================================================
--- test/clang-tidy/readability-redundant-string-init.cpp
+++ test/clang-tidy/readability-redundant-string-init.cpp
@@ -131,3 +131,10 @@
 
   std::string d = "u", e = "u", f = "u";
 }
+
+// These cases should not generate warnings.
+extern void Param1(std::string param = "");
+extern void Param2(const std::string& param = "");
+void Param3(std::string param = "") {}
+void Param4(STRING param = "") {}
+
Index: clang-tidy/readability/RedundantStringInitCheck.cpp
===================================================================
--- clang-tidy/readability/RedundantStringInitCheck.cpp
+++ clang-tidy/readability/RedundantStringInitCheck.cpp
@@ -61,7 +61,8 @@
                         hasInitializer(
                             expr(anyOf(EmptyStringCtorExpr,
                                        EmptyStringCtorExprWithTemporaries))
-                            .bind("expr"))))
+                            .bind("expr"))),
+                unless(parmVarDecl()))
           .bind("decl"),
       this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18829.52806.patch
Type: text/x-patch
Size: 1146 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160406/50860c2d/attachment.bin>


More information about the cfe-commits mailing list