Skip to content

Conversation

@localspook
Copy link
Contributor

@localspook localspook commented Dec 20, 2025

This enables using it like

llvm::scope_exit Cleanup([] { ... });

instead of

auto Cleanup = llvm::make_scope_exit([] { ... });

@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2025

@llvm/pr-subscribers-lldb

@llvm/pr-subscribers-llvm-adt

Author: Victor Chernyakin (localspook)

Changes

This enables using it like

llvm::scope_exit Cleanup = [] { ... };

instead of

auto Cleanup = llvm::make_scope_exit([] { ... });

Full diff: https://github.com/llvm/llvm-project/pull/173131.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/ScopeExit.h (+3-9)
diff --git a/llvm/include/llvm/ADT/ScopeExit.h b/llvm/include/llvm/ADT/ScopeExit.h
index 2f13fb65d34d8..04b602a69f7e3 100644
--- a/llvm/include/llvm/ADT/ScopeExit.h
+++ b/llvm/include/llvm/ADT/ScopeExit.h
@@ -15,13 +15,9 @@
 #ifndef LLVM_ADT_SCOPEEXIT_H
 #define LLVM_ADT_SCOPEEXIT_H
 
-#include "llvm/Support/Compiler.h"
-
-#include <type_traits>
 #include <utility>
 
 namespace llvm {
-namespace detail {
 
 template <typename Callable> class scope_exit {
   Callable ExitFunction;
@@ -47,17 +43,15 @@ template <typename Callable> class scope_exit {
   }
 };
 
-} // end namespace detail
+template <typename Callable> scope_exit(Callable) -> scope_exit<Callable>;
 
 // Keeps the callable object that is passed in, and execute it at the
 // destruction of the returned object (usually at the scope exit where the
 // returned object is kept).
 //
 // Interface is specified by p0052r2.
-template <typename Callable>
-[[nodiscard]] detail::scope_exit<std::decay_t<Callable>>
-make_scope_exit(Callable &&F) {
-  return detail::scope_exit<std::decay_t<Callable>>(std::forward<Callable>(F));
+template <typename Callable> [[nodiscard]] auto make_scope_exit(Callable &&F) {
+  return scope_exit(std::forward<Callable>(F));
 }
 
 } // end namespace llvm

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Do you also plan to mark make_scope_exit as deprecated in a future PR?

@localspook
Copy link
Contributor Author

Yes. I'm hoping to do that after I implement a clang-tidy check (something like modernize-use-ctad) that can do the migration automatically (we already have the logic in abseil-cleanup-ctad, but it only works with one particular abseil class right now).

@localspook localspook merged commit 92e343e into llvm:main Dec 21, 2025
11 checks passed
@localspook localspook deleted the scope-exit-ctad branch December 21, 2025 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants