[clang-tidy] Fix FP with readability-redundant-string-init for default arguments
authorEtienne Bergeron <etienneb@google.com>
Thu, 7 Apr 2016 14:18:53 +0000 (14:18 +0000)
committerEtienne Bergeron <etienneb@google.com>
Thu, 7 Apr 2016 14:18:53 +0000 (14:18 +0000)
Summary:
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 = "");
```

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D18829

llvm-svn: 265671

clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
clang-tools-extra/test/clang-tidy/readability-redundant-string-init.cpp

index d49c55e..2997e5d 100644 (file)
@@ -61,7 +61,8 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
                         hasInitializer(
                             expr(anyOf(EmptyStringCtorExpr,
                                        EmptyStringCtorExprWithTemporaries))
-                            .bind("expr"))))
+                            .bind("expr"))),
+                unless(parmVarDecl()))
           .bind("decl"),
       this);
 }
index 1ebba29..4455ad4 100644 (file)
@@ -131,3 +131,10 @@ void k() {
 
   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 = "") {}
+