steveire created this revision.
steveire added reviewers: aaron.ballman, njames93.
Herald added subscribers: nullptr.cpp, xazax.hun.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The normalization of matchers means that this now works in all language
modes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96135

Files:
  clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
  clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
  
clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp
===================================================================
--- 
clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp
+++ 
clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++14 %s modernize-return-braced-init-list %t
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++14-or-later %s 
modernize-return-braced-init-list %t
 
 namespace std {
 typedef decltype(sizeof(int)) size_t;
Index: clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
+++ clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
@@ -29,6 +29,9 @@
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 };
 
 } // namespace modernize
Index: clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
@@ -23,21 +23,14 @@
   auto ConstructExpr =
       cxxConstructExpr(
           unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
-                       isListInitialization(), hasDescendant(initListExpr()),
-                       isInTemplateInstantiation())))
+                       isListInitialization(), hasDescendant(initListExpr()))))
           .bind("ctor");
 
-  auto CtorAsArgument = materializeTemporaryExpr(anyOf(
-      has(ConstructExpr), has(cxxFunctionalCastExpr(has(ConstructExpr)))));
-
   Finder->addMatcher(
-      traverse(TK_AsIs,
-               functionDecl(
-                   isDefinition(), // Declarations don't have return 
statements.
-                   returns(unless(anyOf(builtinType(), autoType()))),
-                   hasDescendant(returnStmt(hasReturnValue(
-                       has(cxxConstructExpr(has(CtorAsArgument)))))))
-                   .bind("fn")),
+      returnStmt(hasReturnValue(ConstructExpr),
+                 forFunction(functionDecl(returns(unless(anyOf(builtinType(),
+                                                               autoType()))))
+                                 .bind("fn"))),
       this);
 }
 


Index: clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-return-braced-init-list.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++14 %s modernize-return-braced-init-list %t
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-return-braced-init-list %t
 
 namespace std {
 typedef decltype(sizeof(int)) size_t;
Index: clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
+++ clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.h
@@ -29,6 +29,9 @@
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 };
 
 } // namespace modernize
Index: clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp
@@ -23,21 +23,14 @@
   auto ConstructExpr =
       cxxConstructExpr(
           unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
-                       isListInitialization(), hasDescendant(initListExpr()),
-                       isInTemplateInstantiation())))
+                       isListInitialization(), hasDescendant(initListExpr()))))
           .bind("ctor");
 
-  auto CtorAsArgument = materializeTemporaryExpr(anyOf(
-      has(ConstructExpr), has(cxxFunctionalCastExpr(has(ConstructExpr)))));
-
   Finder->addMatcher(
-      traverse(TK_AsIs,
-               functionDecl(
-                   isDefinition(), // Declarations don't have return statements.
-                   returns(unless(anyOf(builtinType(), autoType()))),
-                   hasDescendant(returnStmt(hasReturnValue(
-                       has(cxxConstructExpr(has(CtorAsArgument)))))))
-                   .bind("fn")),
+      returnStmt(hasReturnValue(ConstructExpr),
+                 forFunction(functionDecl(returns(unless(anyOf(builtinType(),
+                                                               autoType()))))
+                                 .bind("fn"))),
       this);
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D96135: [clang-tidy]... Stephen Kelly via Phabricator via cfe-commits

Reply via email to