[PATCH] D96135: [clang-tidy] Simplify braced init check

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 5 06:39:38 PST 2021


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);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96135.321742.patch
Type: text/x-patch
Size: 2709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210205/c0017798/attachment.bin>


More information about the cfe-commits mailing list