From 07473bfd2e4076588bdca73e492974497356151c Mon Sep 17 00:00:00 2001 From: PeterN Date: Sat, 13 May 2023 22:27:32 +0100 Subject: [PATCH] Fix: Don't use a loop to test if classid is valid. (#10818) Additionally the Object class test was broken. --- src/object_gui.cpp | 15 ++------------- src/rail_gui.cpp | 9 +-------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/object_gui.cpp b/src/object_gui.cpp index 5ccfa8f360..97391594d2 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -182,19 +182,8 @@ public: } else { /* Check if the previously selected object class is not available anymore as a * result of starting a new game without the corresponding NewGRF. */ - bool available = false; - for (uint i = 0; ObjectClass::GetClassCount(); ++i) { - if ((ObjectClassID)i == _selected_object_class) { - available = true; - break; - } - } - - if (available) { - this->SelectOtherClass(_selected_object_class); - } else { - this->SelectOtherClass(this->object_classes[0]); - } + bool available = _selected_object_class < ObjectClass::GetClassCount(); + this->SelectOtherClass(available ? _selected_object_class : this->object_classes[0]); } if (this->CanRestoreSelectedObject()) { diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 970f66129c..e3ac337845 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1089,14 +1089,7 @@ public: } else { /* Check if the previously selected station class is not available anymore as a * result of starting a new game without the corresponding NewGRF. */ - bool available = false; - for (uint i = 0; i < StationClass::GetClassCount(); ++i) { - if ((StationClassID)i == _railstation.station_class) { - available = true; - break; - } - } - + bool available = _railstation.station_class < StationClass::GetClassCount(); this->SelectOtherClass(available ? _railstation.station_class : StationClassID::STAT_CLASS_DFLT); } }