[PATCH] Fix ExplicitConstructorCheck to warn only on in-class declarations.

Alexander Kornienko alexfh at google.com
Thu Feb 13 02:11:22 PST 2014


  Re-implemented without the new matchers.

Hi djasper, klimek,

http://llvm-reviews.chandlerc.com/D2756

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2756?vs=7049&id=7050#toc

Files:
  clang-tidy/google/GoogleTidyModule.cpp
  unittests/clang-tidy/GoogleModuleTest.cpp

Index: clang-tidy/google/GoogleTidyModule.cpp
===================================================================
--- clang-tidy/google/GoogleTidyModule.cpp
+++ clang-tidy/google/GoogleTidyModule.cpp
@@ -24,20 +24,20 @@
 namespace clang {
 namespace tidy {
 
-void
-ExplicitConstructorCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
-  Finder->addMatcher(constructorDecl().bind("construct"), this);
+void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(constructorDecl().bind("ctor"), this);
 }
 
 void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
   const CXXConstructorDecl *Ctor =
-      Result.Nodes.getNodeAs<CXXConstructorDecl>("construct");
-  if (!Ctor->isExplicit() && !Ctor->isImplicit() && Ctor->getNumParams() >= 1 &&
-      Ctor->getMinRequiredArguments() <= 1) {
-    SourceLocation Loc = Ctor->getLocation();
-    diag(Loc, "Single-argument constructors must be explicit")
-        << FixItHint::CreateInsertion(Loc, "explicit ");
-  }
+      Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
+  if (Ctor->isOutOfLine() || Ctor->isExplicit() || Ctor->isImplicit())
+    return;
+  if (Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
+    return;
+  SourceLocation Loc = Ctor->getLocation();
+  diag(Loc, "Single-argument constructors must be explicit")
+      << FixItHint::CreateInsertion(Loc, "explicit ");
 }
 
 class GoogleModule : public ClangTidyModule {
Index: unittests/clang-tidy/GoogleModuleTest.cpp
===================================================================
--- unittests/clang-tidy/GoogleModuleTest.cpp
+++ unittests/clang-tidy/GoogleModuleTest.cpp
@@ -21,5 +21,10 @@
             runCheckOn("class C { C(int i, int j = 0); };"));
 }
 
+TEST_F(ExplicitConstructorCheckTest, OutOfLineDefinitions) {
+  EXPECT_EQ("class C { explicit C(int i); }; C::C(int i) {}",
+            runCheckOn("class C { C(int i); }; C::C(int i) {}"));
+}
+
 } // namespace tidy
 } // namespace clang
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2756.3.patch
Type: text/x-patch
Size: 2014 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140213/a6e04660/attachment.bin>


More information about the cfe-commits mailing list