From d64969968272604015f1fd07e8cb0361c2bd4214 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Thu, 15 Oct 2026 15:30:08 +0000 Subject: [PATCH] Disallow copy of SaveAndRestore (#7966) An accidental copy would cause an extra save/restore. This is meant as an RAII object to denote a save for a given scope. Also disallow move so we don't need to add branches to the dtor. --- common/checked_save_and_restore.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/common/checked_save_and_restore.h b/common/checked_save_and_restore.h index 10ecfca1ed9e..aee93437a9af 100644 --- a/common/checked_save_and_restore.h +++ b/common/checked_save_and_restore.h @@ -12,11 +12,12 @@ namespace Carbon { // Sets a variable to a new value, and restores the original value when -// the SetAndRestore is destroyed. CHECKs that the variable is still holding the -// new value when restoring the original value, to catch any data loss. +// the CheckedSaveAndRestore is destroyed. CHECKs that the variable is still +// holding the new value when restoring the original value, to catch any data +// loss. // // The value must be copyable in order to compare with the new value at the time -// the SetAndRestore is destroyed. +// the CheckedSaveAndRestore is destroyed. template class [[nodiscard]] CheckedSaveAndRestore { public: @@ -30,6 +31,10 @@ class [[nodiscard]] CheckedSaveAndRestore { *var_ = std::move(orig_value_); } + CheckedSaveAndRestore(const CheckedSaveAndRestore&) = delete; + auto operator=(const CheckedSaveAndRestore&) + -> CheckedSaveAndRestore& = delete; + private: T* var_; T new_value_;