diff --git a/clang-tools-extra/clang-tidy/abseil/CleanupCtadCheck.cpp b/clang-tools-extra/clang-tidy/abseil/CleanupCtadCheck.cpp --- a/clang-tools-extra/clang-tidy/abseil/CleanupCtadCheck.cpp +++ b/clang-tools-extra/clang-tidy/abseil/CleanupCtadCheck.cpp @@ -28,16 +28,14 @@ "deduction pattern in C++17 and higher"); return makeRule( - declStmt(has(varDecl( + declStmt(hasSingleDecl(varDecl( hasType(autoType()), hasTypeLoc(typeLoc().bind("auto_type_loc")), - hasInitializer(traverse( - clang::TK_IgnoreUnlessSpelledInSource, + hasInitializer(hasDescendant( callExpr(callee(functionDecl(hasName("absl::MakeCleanup"))), - argumentCountIs(1), - hasArgument(0, expr().bind("make_cleanup_argument"))) + argumentCountIs(1)) .bind("make_cleanup_call")))))), {changeTo(node("auto_type_loc"), cat("absl::Cleanup")), - changeTo(node("make_cleanup_call"), cat(node("make_cleanup_argument")))}, + changeTo(node("make_cleanup_call"), cat(callArgs("make_cleanup_call")))}, warning_message); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/abseil-cleanup-ctad.cpp b/clang-tools-extra/test/clang-tidy/checkers/abseil-cleanup-ctad.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/abseil-cleanup-ctad.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/abseil-cleanup-ctad.cpp @@ -81,35 +81,25 @@ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer absl::Cleanup's class template argument deduction pattern in C++17 and higher // CHECK-FIXES: {{^}} absl::Cleanup a = [] {};{{$}} - // Removes extra parens - auto b = absl::MakeCleanup(([] {})); + auto b = absl::MakeCleanup(std::function([] {})); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} absl::Cleanup b = [] {};{{$}} + // CHECK-FIXES: {{^}} absl::Cleanup b = std::function([] {});{{$}} - auto c = absl::MakeCleanup(std::function([] {})); - // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} absl::Cleanup c = std::function([] {});{{$}} - - // Removes extra parens - auto d = absl::MakeCleanup((std::function([] {}))); - // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} absl::Cleanup d = std::function([] {});{{$}} - - const auto e = absl::MakeCleanup([] {}); + const auto c = absl::MakeCleanup([] {}); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} const absl::Cleanup e = [] {};{{$}} + // CHECK-FIXES: {{^}} const absl::Cleanup c = [] {};{{$}} - // Removes extra parens - const auto f = absl::MakeCleanup(([] {})); + const auto d = absl::MakeCleanup(std::function([] {})); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} const absl::Cleanup f = [] {};{{$}} + // CHECK-FIXES: {{^}} const absl::Cleanup d = std::function([] {});{{$}} - const auto g = absl::MakeCleanup(std::function([] {})); - // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} const absl::Cleanup g = std::function([] {});{{$}} + // Preserves extra parens + auto e = absl::MakeCleanup(([] {})); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer absl::Cleanup{{.*}}C++17 and higher + // CHECK-FIXES: {{^}} absl::Cleanup e = ([] {});{{$}} - // Removes extra parens - const auto h = absl::MakeCleanup((std::function([] {}))); - // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: prefer absl::Cleanup{{.*}}C++17 and higher - // CHECK-FIXES: {{^}} const absl::Cleanup h = std::function([] {});{{$}} + // Preserves comments + auto f = /* a */ absl::MakeCleanup(/* b */ [] { /* c */ } /* d */) /* e */; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer absl::Cleanup{{.*}}C++17 and higher + // CHECK-FIXES: {{^}} absl::Cleanup f = /* a */ /* b */ [] { /* c */ } /* d */ /* e */;{{$}} }