1
0
Fork 0

Codechange: Add 'const' to static variables, which are only initialised once.

pull/14226/head
frosch 2025-04-27 16:35:43 +02:00 committed by frosch
parent 1900125c98
commit 61cec33be2
20 changed files with 34 additions and 34 deletions

View File

@ -2036,7 +2036,7 @@ static bool ConSayClient(std::span<std::string_view> argv)
} }
/** All the known authorized keys with their name. */ /** All the known authorized keys with their name. */
static std::vector<std::pair<std::string_view, NetworkAuthorizedKeys *>> _console_cmd_authorized_keys{ static const std::initializer_list<std::pair<std::string_view, NetworkAuthorizedKeys *>> _console_cmd_authorized_keys{
{ "admin", &_settings_client.network.admin_authorized_keys }, { "admin", &_settings_client.network.admin_authorized_keys },
{ "rcon", &_settings_client.network.rcon_authorized_keys }, { "rcon", &_settings_client.network.rcon_authorized_keys },
{ "server", &_settings_client.network.server_authorized_keys }, { "server", &_settings_client.network.server_authorized_keys },

View File

@ -540,7 +540,7 @@ struct EffectProcs {
}; };
/** Per-EffectVehicleType handling. */ /** Per-EffectVehicleType handling. */
static std::array<EffectProcs, EV_END> _effect_procs = {{ static const std::array<EffectProcs, EV_END> _effect_procs = {{
{ ChimneySmokeInit, ChimneySmokeTick, TO_INDUSTRIES }, // EV_CHIMNEY_SMOKE { ChimneySmokeInit, ChimneySmokeTick, TO_INDUSTRIES }, // EV_CHIMNEY_SMOKE
{ SteamSmokeInit, SteamSmokeTick, TO_INVALID }, // EV_STEAM_SMOKE { SteamSmokeInit, SteamSmokeTick, TO_INVALID }, // EV_STEAM_SMOKE
{ DieselSmokeInit, DieselSmokeTick, TO_INVALID }, // EV_DIESEL_SMOKE { DieselSmokeInit, DieselSmokeTick, TO_INVALID }, // EV_DIESEL_SMOKE

View File

@ -716,7 +716,7 @@ FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(p
static std::string _prefix; ///< Static as the lambda needs access to it. static std::string _prefix; ///< Static as the lambda needs access to it.
/* Callback for FiosFileScanner. */ /* Callback for FiosFileScanner. */
static FiosGetTypeAndNameProc *proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) { static FiosGetTypeAndNameProc *const proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) {
if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{}); if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
return std::tuple(FIOS_TYPE_INVALID, std::string{}); return std::tuple(FIOS_TYPE_INVALID, std::string{});
}; };

View File

@ -48,7 +48,7 @@ struct IniGroup {
/** Ini file that only supports loading. */ /** Ini file that only supports loading. */
struct IniLoadFile { struct IniLoadFile {
using IniGroupNameList = std::initializer_list<std::string_view>; using IniGroupNameList = std::initializer_list<const std::string_view>;
std::list<IniGroup> groups; ///< all groups in the ini std::list<IniGroup> groups; ///< all groups in the ini
std::string comment; ///< last comment in file std::string comment; ///< last comment in file

View File

@ -178,7 +178,7 @@ struct DumpTarget {
/** Dump nested object (or only its name if this instance is already known). */ /** Dump nested object (or only its name if this instance is already known). */
template <typename S> void WriteStructT(std::string_view name, const S *s) template <typename S> void WriteStructT(std::string_view name, const S *s)
{ {
static size_t type_id = ++LastTypeId(); static const size_t type_id = ++LastTypeId();
if (s == nullptr) { if (s == nullptr) {
/* No need to dump nullptr struct. */ /* No need to dump nullptr struct. */
@ -201,7 +201,7 @@ struct DumpTarget {
/** Dump nested object (or only its name if this instance is already known). */ /** Dump nested object (or only its name if this instance is already known). */
template <typename S> void WriteStructT(std::string_view name, const std::deque<S> *s) template <typename S> void WriteStructT(std::string_view name, const std::deque<S> *s)
{ {
static size_t type_id = ++LastTypeId(); static const size_t type_id = ++LastTypeId();
if (s == nullptr) { if (s == nullptr) {
/* No need to dump nullptr struct. */ /* No need to dump nullptr struct. */

View File

@ -29,19 +29,19 @@
#if defined(UNIX) #if defined(UNIX)
/** List of certificate bundles, depending on OS. Taken from: https://go.dev/src/crypto/x509/root_linux.go. */ /** List of certificate bundles, depending on OS. Taken from: https://go.dev/src/crypto/x509/root_linux.go. */
static auto _certificate_files = { static constexpr std::initializer_list<std::string_view> _certificate_files = {
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc. "/etc/ssl/certs/ca-certificates.crt"sv, // Debian/Ubuntu/Gentoo etc.
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6 "/etc/pki/tls/certs/ca-bundle.crt"sv, // Fedora/RHEL 6
"/etc/ssl/ca-bundle.pem", // OpenSUSE "/etc/ssl/ca-bundle.pem"sv, // OpenSUSE
"/etc/pki/tls/cacert.pem", // OpenELEC "/etc/pki/tls/cacert.pem"sv, // OpenELEC
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"sv, // CentOS/RHEL 7
"/etc/ssl/cert.pem", // Alpine Linux "/etc/ssl/cert.pem"sv, // Alpine Linux
}; };
/** List of certificate directories, depending on OS. Taken from: https://go.dev/src/crypto/x509/root_linux.go. */ /** List of certificate directories, depending on OS. Taken from: https://go.dev/src/crypto/x509/root_linux.go. */
static auto _certificate_directories = { static constexpr std::initializer_list<std::string_view> _certificate_directories = {
"/etc/ssl/certs", // SLES10/SLES11, https://golang.org/issue/12139 "/etc/ssl/certs"sv, // SLES10/SLES11, https://golang.org/issue/12139
"/etc/pki/tls/certs", // Fedora/RHEL "/etc/pki/tls/certs"sv, // Fedora/RHEL
"/system/etc/security/cacerts", // Android "/system/etc/security/cacerts"sv, // Android
}; };
#endif /* UNIX */ #endif /* UNIX */

View File

@ -110,7 +110,7 @@ inline auto MakeCallbackTable(std::index_sequence<i...>) noexcept
} }
/** Type-erased table of callbacks. */ /** Type-erased table of callbacks. */
static auto _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{}); static const auto _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{});
template <typename T> struct CallbackArgsHelper; template <typename T> struct CallbackArgsHelper;
template <typename... Targs> template <typename... Targs>

View File

@ -317,7 +317,7 @@ static WindowDesc &GetNewsWindowLayout(NewsStyle style)
/** /**
* Per-NewsType data * Per-NewsType data
*/ */
static NewsTypeData _news_type_data[] = { static const NewsTypeData _news_type_data[] = {
/* name, age, sound, */ /* name, age, sound, */
NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE ), ///< NewsType::ArrivalCompany NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE ), ///< NewsType::ArrivalCompany
NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE ), ///< NewsType::ArrivalOther NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE ), ///< NewsType::ArrivalOther

View File

@ -145,7 +145,7 @@ static CGFloat SpriteFontGetWidth(void *ref_con)
return GetGlyphWidth(fs, c); return GetGlyphWidth(fs, c);
} }
static CTRunDelegateCallbacks _sprite_font_callback = { static const CTRunDelegateCallbacks _sprite_font_callback = {
kCTRunDelegateCurrentVersion, nullptr, nullptr, nullptr, kCTRunDelegateCurrentVersion, nullptr, nullptr, nullptr,
&SpriteFontGetWidth &SpriteFontGetWidth
}; };
@ -330,7 +330,7 @@ void MacOSSetCurrentLocaleName(std::string_view iso_code)
*/ */
int MacOSStringCompare(std::string_view s1, std::string_view s2) int MacOSStringCompare(std::string_view s1, std::string_view s2)
{ {
static bool supported = MacOSVersionIsAtLeast(10, 5, 0); static const bool supported = MacOSVersionIsAtLeast(10, 5, 0);
if (!supported) return 0; if (!supported) return 0;
CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
@ -354,7 +354,7 @@ int MacOSStringCompare(std::string_view s1, std::string_view s2)
*/ */
int MacOSStringContains(std::string_view str, std::string_view value, bool case_insensitive) int MacOSStringContains(std::string_view str, std::string_view value, bool case_insensitive)
{ {
static bool supported = MacOSVersionIsAtLeast(10, 5, 0); static const bool supported = MacOSVersionIsAtLeast(10, 5, 0);
if (!supported) return -1; if (!supported) return -1;
CFStringCompareFlags flags = kCFCompareLocalized | kCFCompareWidthInsensitive; CFStringCompareFlags flags = kCFCompareLocalized | kCFCompareWidthInsensitive;

View File

@ -66,7 +66,7 @@ static constexpr EndSegmentReasons ESRF_ABORT_PF_MASK = {
inline std::string ValueStr(EndSegmentReasons flags) inline std::string ValueStr(EndSegmentReasons flags)
{ {
static const std::initializer_list<std::string_view> end_segment_reason_names = { static const std::initializer_list<const std::string_view> end_segment_reason_names = {
"DEAD_END", "RAIL_TYPE", "INFINITE_LOOP", "SEGMENT_TOO_LONG", "CHOICE_FOLLOWS", "DEAD_END", "RAIL_TYPE", "INFINITE_LOOP", "SEGMENT_TOO_LONG", "CHOICE_FOLLOWS",
"DEPOT", "WAYPOINT", "STATION", "SAFE_TILE", "DEPOT", "WAYPOINT", "STATION", "SAFE_TILE",
"PATH_TOO_LONG", "FIRST_TWO_WAY_RED", "LOOK_AHEAD_END", "TARGET_REACHED" "PATH_TOO_LONG", "FIRST_TWO_WAY_RED", "LOOK_AHEAD_END", "TARGET_REACHED"

View File

@ -1916,7 +1916,7 @@ struct FenceOffset {
}; };
/** Offsets for drawing fences */ /** Offsets for drawing fences */
static FenceOffset _fence_offsets[] = { static const FenceOffset _fence_offsets[] = {
{ CORNER_INVALID, 0, 1, 16, 1 }, // RFO_FLAT_X_NW { CORNER_INVALID, 0, 1, 16, 1 }, // RFO_FLAT_X_NW
{ CORNER_INVALID, 1, 0, 1, 16 }, // RFO_FLAT_Y_NE { CORNER_INVALID, 1, 0, 1, 16 }, // RFO_FLAT_Y_NE
{ CORNER_W, 8, 8, 1, 1 }, // RFO_FLAT_LEFT { CORNER_W, 8, 8, 1, 1 }, // RFO_FLAT_LEFT

View File

@ -153,4 +153,4 @@ public:
} }
}; };
static ScreenshotProvider_Pcx s_screenshot_provider_pcx; static const ScreenshotProvider_Pcx s_screenshot_provider_pcx;

View File

@ -1425,7 +1425,7 @@ void LoadFromConfig(bool startup)
} }
if (generic_version < IFV_AUTOSAVE_RENAME && IsConversionNeeded(generic_ini, "gui", "autosave", "autosave_interval", &old_item)) { if (generic_version < IFV_AUTOSAVE_RENAME && IsConversionNeeded(generic_ini, "gui", "autosave", "autosave_interval", &old_item)) {
static std::vector<std::string_view> _old_autosave_interval{"off", "monthly", "quarterly", "half year", "yearly"}; static constexpr std::initializer_list<std::string_view> _old_autosave_interval{"off"sv, "monthly"sv, "quarterly"sv, "half year"sv, "yearly"sv};
auto old_value = OneOfManySettingDesc::ParseSingleValue(*old_item->value, _old_autosave_interval).value_or(-1); auto old_value = OneOfManySettingDesc::ParseSingleValue(*old_item->value, _old_autosave_interval).value_or(-1);
switch (old_value) { switch (old_value) {

View File

@ -448,7 +448,7 @@ static bool CheckRoadSide(int32_t &)
static std::optional<uint32_t> ConvertLandscape(std::string_view value) static std::optional<uint32_t> ConvertLandscape(std::string_view value)
{ {
/* try with the old values */ /* try with the old values */
static std::vector<std::string_view> _old_landscape_values{"normal", "hilly", "desert", "candy"}; static constexpr std::initializer_list<std::string_view> _old_landscape_values{"normal"sv, "hilly"sv, "desert"sv, "candy"sv};
return OneOfManySettingDesc::ParseSingleValue(value, _old_landscape_values); return OneOfManySettingDesc::ParseSingleValue(value, _old_landscape_values);
} }

View File

@ -55,7 +55,7 @@ struct LegendAndColour {
}; };
/** Link stat colours shown in legenda. */ /** Link stat colours shown in legenda. */
static uint8_t _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11}; static const uint8_t _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies. static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.

View File

@ -864,7 +864,7 @@ std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, std:
std::string_view base_path = filename.substr(0, slash + 1); std::string_view base_path = filename.substr(0, slash + 1);
static const std::initializer_list<std::string_view> extensions{ static const std::initializer_list<const std::string_view> extensions{
"txt", "txt",
"md", "md",
#if defined(WITH_ZLIB) #if defined(WITH_ZLIB)

View File

@ -961,7 +961,7 @@ static void MakeCatalanTownName(StringBuilder &builder, uint32_t seed)
typedef void TownNameGenerator(StringBuilder &builder, uint32_t seed); typedef void TownNameGenerator(StringBuilder &builder, uint32_t seed);
/** Town name generators */ /** Town name generators */
static TownNameGenerator *_town_name_generators[] = { static TownNameGenerator *const _town_name_generators[] = {
MakeEnglishOriginalTownName, // replaces first 4 characters of name MakeEnglishOriginalTownName, // replaces first 4 characters of name
MakeFrenchTownName, MakeFrenchTownName,
MakeGermanTownName, MakeGermanTownName,

View File

@ -2391,13 +2391,13 @@ extern void DrawRoadVehDetails(const Vehicle *v, const Rect &r);
extern void DrawShipDetails(const Vehicle *v, const Rect &r); extern void DrawShipDetails(const Vehicle *v, const Rect &r);
extern void DrawAircraftDetails(const Aircraft *v, const Rect &r); extern void DrawAircraftDetails(const Aircraft *v, const Rect &r);
static StringID _service_interval_dropdown_calendar[] = { static const StringID _service_interval_dropdown_calendar[] = {
STR_VEHICLE_DETAILS_DEFAULT, STR_VEHICLE_DETAILS_DEFAULT,
STR_VEHICLE_DETAILS_DAYS, STR_VEHICLE_DETAILS_DAYS,
STR_VEHICLE_DETAILS_PERCENT, STR_VEHICLE_DETAILS_PERCENT,
}; };
static StringID _service_interval_dropdown_wallclock[] = { static const StringID _service_interval_dropdown_wallclock[] = {
STR_VEHICLE_DETAILS_DEFAULT, STR_VEHICLE_DETAILS_DEFAULT,
STR_VEHICLE_DETAILS_MINUTES, STR_VEHICLE_DETAILS_MINUTES,
STR_VEHICLE_DETAILS_PERCENT, STR_VEHICLE_DETAILS_PERCENT,

View File

@ -442,7 +442,7 @@ static void SetDarkModeForWindow(HWND hWnd, bool dark_mode)
* reason, the code uses dynamic loading and ignores any errors for a best-effort result. */ * reason, the code uses dynamic loading and ignores any errors for a best-effort result. */
static LibraryLoader _dwmapi("dwmapi.dll"); static LibraryLoader _dwmapi("dwmapi.dll");
typedef HRESULT(WINAPI *PFNDWMSETWINDOWATTRIBUTE)(HWND, DWORD, LPCVOID, DWORD); typedef HRESULT(WINAPI *PFNDWMSETWINDOWATTRIBUTE)(HWND, DWORD, LPCVOID, DWORD);
static PFNDWMSETWINDOWATTRIBUTE DwmSetWindowAttribute = _dwmapi.GetFunction("DwmSetWindowAttribute"); static const PFNDWMSETWINDOWATTRIBUTE DwmSetWindowAttribute = _dwmapi.GetFunction("DwmSetWindowAttribute");
if (DwmSetWindowAttribute != nullptr) { if (DwmSetWindowAttribute != nullptr) {
/* Contrary to the published documentation, DWMWA_USE_IMMERSIVE_DARK_MODE does not change the /* Contrary to the published documentation, DWMWA_USE_IMMERSIVE_DARK_MODE does not change the

View File

@ -3559,7 +3559,7 @@ struct ViewportSSCSS {
}; };
/** List of sorters ordered from best to worst. */ /** List of sorters ordered from best to worst. */
static ViewportSSCSS _vp_sprite_sorters[] = { static const ViewportSSCSS _vp_sprite_sorters[] = {
#ifdef WITH_SSE #ifdef WITH_SSE
{ &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 }, { &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 },
#endif #endif