From c283e11ffa879d7ae214531b6f5025afb177f4f7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 26 Mar 2026 13:32:32 -1000 Subject: [PATCH] Fix clang-tidy: add braces around if statements, extract ensure helpers --- esphome/components/climate/climate.h | 42 +++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/esphome/components/climate/climate.h b/esphome/components/climate/climate.h index 69ea192456e..8e369369570 100644 --- a/esphome/components/climate/climate.h +++ b/esphome/components/climate/climate.h @@ -236,37 +236,24 @@ class Climate : public EntityBase { /// Set the supported custom fan modes (stored on Climate, referenced by ClimateTraits). void set_supported_custom_fan_modes(std::initializer_list modes) { - if (!this->supported_custom_fan_modes_) - this->supported_custom_fan_modes_ = - new std::vector(); // NOLINT - intentional leak, entity lives forever - *this->supported_custom_fan_modes_ = modes; + this->ensure_custom_fan_modes_().assign(modes.begin(), modes.end()); } void set_supported_custom_fan_modes(const std::vector &modes) { - if (!this->supported_custom_fan_modes_) - this->supported_custom_fan_modes_ = new std::vector(); // NOLINT - *this->supported_custom_fan_modes_ = modes; + this->ensure_custom_fan_modes_() = modes; } template void set_supported_custom_fan_modes(const char *const (&modes)[N]) { - if (!this->supported_custom_fan_modes_) - this->supported_custom_fan_modes_ = new std::vector(); // NOLINT - this->supported_custom_fan_modes_->assign(modes, modes + N); + this->ensure_custom_fan_modes_().assign(modes, modes + N); } /// Set the supported custom presets (stored on Climate, referenced by ClimateTraits). void set_supported_custom_presets(std::initializer_list presets) { - if (!this->supported_custom_presets_) - this->supported_custom_presets_ = new std::vector(); // NOLINT - *this->supported_custom_presets_ = presets; + this->ensure_custom_presets_().assign(presets.begin(), presets.end()); } void set_supported_custom_presets(const std::vector &presets) { - if (!this->supported_custom_presets_) - this->supported_custom_presets_ = new std::vector(); // NOLINT - *this->supported_custom_presets_ = presets; + this->ensure_custom_presets_() = presets; } template void set_supported_custom_presets(const char *const (&presets)[N]) { - if (!this->supported_custom_presets_) - this->supported_custom_presets_ = new std::vector(); // NOLINT - this->supported_custom_presets_->assign(presets, presets + N); + this->ensure_custom_presets_().assign(presets, presets + N); } /// Check if a custom fan mode is currently active. @@ -379,9 +366,20 @@ class Climate : public EntityBase { LazyCallbackManager control_callback_{}; ESPPreferenceObject rtc_; - /** Custom mode storage — allocated on first use, never freed (entity lives forever). - * Pointers in these vectors must point to string literals or static data. - */ + /// Lazy-allocate custom mode vectors (never freed — entity lives forever). + std::vector &ensure_custom_fan_modes_() { + if (!this->supported_custom_fan_modes_) { + this->supported_custom_fan_modes_ = new std::vector(); // NOLINT + } + return *this->supported_custom_fan_modes_; + } + std::vector &ensure_custom_presets_() { + if (!this->supported_custom_presets_) { + this->supported_custom_presets_ = new std::vector(); // NOLINT + } + return *this->supported_custom_presets_; + } + std::vector *supported_custom_fan_modes_{nullptr}; std::vector *supported_custom_presets_{nullptr};