diff --git a/src/blitter/32bpp_simple.cpp b/src/blitter/32bpp_simple.cpp index 653b2a4b38..9fcb224b08 100644 --- a/src/blitter/32bpp_simple.cpp +++ b/src/blitter/32bpp_simple.cpp @@ -118,7 +118,7 @@ void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height Sprite *Blitter_32bppSimple::Encode(const SpriteLoader::SpriteCollection &sprite, AllocatorProc *allocator) { Blitter_32bppSimple::Pixel *dst; - Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite[ZOOM_LVL_MIN].height * (size_t)sprite[ZOOM_LVL_MIN].width * sizeof(*dst)); + Sprite *dest_sprite = static_cast(allocator(sizeof(*dest_sprite) + static_cast(sprite[ZOOM_LVL_MIN].height) * static_cast(sprite[ZOOM_LVL_MIN].width) * sizeof(*dst))); dest_sprite->height = sprite[ZOOM_LVL_MIN].height; dest_sprite->width = sprite[ZOOM_LVL_MIN].width; diff --git a/src/blitter/8bpp_simple.cpp b/src/blitter/8bpp_simple.cpp index 58c7cb36fa..8dfa599a61 100644 --- a/src/blitter/8bpp_simple.cpp +++ b/src/blitter/8bpp_simple.cpp @@ -64,7 +64,7 @@ void Blitter_8bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, Zoom Sprite *Blitter_8bppSimple::Encode(const SpriteLoader::SpriteCollection &sprite, AllocatorProc *allocator) { Sprite *dest_sprite; - dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite[ZOOM_LVL_MIN].height * (size_t)sprite[ZOOM_LVL_MIN].width); + dest_sprite = static_cast(allocator(sizeof(*dest_sprite) + static_cast(sprite[ZOOM_LVL_MIN].height) * static_cast(sprite[ZOOM_LVL_MIN].width))); dest_sprite->height = sprite[ZOOM_LVL_MIN].height; dest_sprite->width = sprite[ZOOM_LVL_MIN].width; diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 496d945934..98b483ef97 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -180,11 +180,11 @@ struct IConsoleWindow : Window void Scroll(int amount) { if (amount < 0) { - size_t namount = (size_t) -amount; + size_t namount = static_cast(-amount); IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount; } else { assert(this->height >= 0 && this->line_height > 0); - size_t visible_lines = (size_t)(this->height / this->line_height); + size_t visible_lines = static_cast(this->height / this->line_height); size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines; IConsoleWindow::scroll = std::min(IConsoleWindow::scroll + amount, max_scroll); } @@ -223,7 +223,7 @@ struct IConsoleWindow : Window /** Check on a regular interval if the console buffer needs truncating. */ IntervalTimer truncate_interval = {std::chrono::seconds(3), [this](auto) { assert(this->height >= 0 && this->line_height > 0); - size_t visible_lines = (size_t)(this->height / this->line_height); + size_t visible_lines = static_cast(this->height / this->line_height); if (TruncateBuffer() && IConsoleWindow::scroll + visible_lines > _iconsole_buffer.size()) { size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines; diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index 91280e5555..dbead01676 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -260,17 +260,17 @@ Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const const auto &charmap = run.GetGlyphToCharMap(); /* Run starts after our character, use the last found position. */ - if ((size_t)charmap.front() > index) return *position; + if (static_cast(charmap.front()) > index) return *position; position = positions.begin(); for (auto it = charmap.begin(); it != charmap.end(); /* nothing */) { /* Plain honest-to-$deity match. */ - if ((size_t)*it == index) return *position; + if (static_cast(*it) == index) return *position; ++it; if (it == charmap.end()) break; /* We just passed our character, it's probably a ligature, use the last found position. */ - if ((size_t)*it > index) return *position; + if (static_cast(*it) > index) return *position; ++position; } } diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 50c25579da..41b52469c1 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -99,7 +99,7 @@ static uint16_t ParseCode(const char *start, const char *end) assert(start <= end); while (start < end && *start == ' ') start++; while (end > start && *end == ' ') end--; - std::string_view str{start, (size_t)(end - start)}; + std::string_view str{start, static_cast(end - start)}; for (const auto &kn : _keycode_to_name) { if (StrEqualsIgnoreCase(str, kn.name)) { return kn.keycode; diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 1382d86556..36bdd1bffb 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -1694,7 +1694,7 @@ static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int t /* On a large map with many industries, it may be faster to check an area. */ static const int dmax = 14; - if (Industry::GetNumItems() > (size_t) (dmax * dmax * 2)) { + if (Industry::GetNumItems() > static_cast(dmax * dmax * 2)) { const Industry *i = nullptr; TileArea tile_area = TileArea(tile, 1, 1).Expand(dmax); for (TileIndex atile : tile_area) { diff --git a/src/ini_load.cpp b/src/ini_load.cpp index 11e2077049..e4209ad3b2 100644 --- a/src/ini_load.cpp +++ b/src/ini_load.cpp @@ -202,7 +202,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir) end += ftell(in); /* for each line in the file */ - while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) { + while (static_cast(ftell(in)) < end && fgets(buffer, sizeof(buffer), in)) { char c, *s; /* trim whitespace from the left side */ for (s = buffer; *s == ' ' || *s == '\t'; s++) {} diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index 32e6c2d34a..092c5964ae 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -184,7 +184,7 @@ struct SelectGameWindow : public Window { this->ReadIntroGameViewportCommands(); - this->cur_viewport_command_index = (size_t)-1; + this->cur_viewport_command_index = SIZE_MAX; this->cur_viewport_command_time = 0; this->mouse_idle_time = 0; this->mouse_idle_pos = _cursor.pos; diff --git a/src/misc/dbg_helpers.h b/src/misc/dbg_helpers.h index fdfcaba04a..357cd0dd47 100644 --- a/src/misc/dbg_helpers.h +++ b/src/misc/dbg_helpers.h @@ -34,7 +34,7 @@ template struct ArrayT { template inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::Item t_unk) { - if ((size_t)idx >= ArrayT::length) { + if (static_cast(idx) >= ArrayT::length) { return t_unk; } return t[idx]; @@ -48,7 +48,7 @@ inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::I template inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::Item t_unk, E idx_inv, typename ArrayT::Item t_inv) { - if ((size_t)idx < ArrayT::length) { + if (static_cast(idx) < ArrayT::length) { return t[idx]; } if (idx == idx_inv) { diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index 974630c63d..8e95f6c30e 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -258,8 +258,8 @@ size_t Packet::Size() const */ bool Packet::ParsePacketSize() { - size_t size = (size_t)this->buffer[0]; - size += (size_t)this->buffer[1] << 8; + size_t size = static_cast(this->buffer[0]); + size += static_cast(this->buffer[1]) << 8; /* If the size of the packet is less than the bytes required for the size and type of * the packet, or more than the allowed limit, then something is wrong with the packet. diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index c7acbd21a1..d3e69fce85 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -133,7 +133,7 @@ void NetworkUDPSocketHandler::ReceivePackets() /* If the size does not match the packet must be corrupted. * Otherwise it will be marked as corrupted later on. */ - if (!p.ParsePacketSize() || (size_t)nbytes != p.Size()) { + if (!p.ParsePacketSize() || static_cast(nbytes) != p.Size()) { Debug(net, 1, "Received a packet with mismatching size from {}", address.GetAddressAsString()); continue; } diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index 27bd3ce730..6db744dffe 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -442,7 +442,7 @@ static bool GunzipFile(const ContentInfo *ci) if (errnum != 0 && errnum != Z_STREAM_END) ret = false; break; } - if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) { + if (read < 0 || static_cast(read) != fwrite(buff, 1, read, fout)) { /* If gzread() returns -1, there was an error in archive */ ret = false; break; @@ -496,7 +496,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet &p) } else { /* We have a file opened, thus are downloading internal content */ size_t toRead = p.RemainingBytesToTransfer(); - if (toRead != 0 && (size_t)p.TransferOut(TransferOutFWrite, this->curFile) != toRead) { + if (toRead != 0 && static_cast(p.TransferOut(TransferOutFWrite, this->curFile)) != toRead) { CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD); ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR); this->CloseConnection(); diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 60c6998b0f..8e249d58bf 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -261,7 +261,7 @@ size_t GRFGetSizeOfDataSection(FILE *f) if (fread(data, 1, header_len, f) == header_len) { if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) { /* Valid container version 2, get data section size. */ - size_t offset = ((size_t)data[13] << 24) | ((size_t)data[12] << 16) | ((size_t)data[11] << 8) | (size_t)data[10]; + size_t offset = (static_cast(data[13]) << 24) | (static_cast(data[12]) << 16) | (static_cast(data[11]) << 8) | static_cast(data[10]); if (offset >= 1 * 1024 * 1024 * 1024) { Debug(grf, 0, "Unexpectedly large offset for NewGRF"); /* Having more than 1 GiB of data is very implausible. Mostly because then diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index b406cbc007..1cfce1cd2b 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -133,7 +133,7 @@ static std::string convert_tofrom_fs(iconv_t convd, const std::string &name) size_t outlen = buf.size(); char *outbuf = buf.data(); iconv(convd, nullptr, nullptr, nullptr, nullptr); - if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (size_t)(-1)) { + if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == SIZE_MAX) { Debug(misc, 0, "[iconv] error converting '{}'. Errno {}", name, errno); return name; } diff --git a/src/random_access_file.cpp b/src/random_access_file.cpp index c02e9b46eb..29764521ed 100644 --- a/src/random_access_file.cpp +++ b/src/random_access_file.cpp @@ -37,7 +37,7 @@ RandomAccessFile::RandomAccessFile(const std::string &filename, Subdirectory sub this->simplified_filename = name_without_path.substr(0, name_without_path.rfind('.')); strtolower(this->simplified_filename); - this->SeekTo((size_t)pos, SEEK_SET); + this->SeekTo(static_cast(pos), SEEK_SET); } /** diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index c56b68f7da..099a2c9c29 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -465,7 +465,7 @@ static inline Colours RemapTTOColour(Colours tto) COLOUR_WHITE, COLOUR_LIGHT_BLUE, COLOUR_MAUVE, COLOUR_PINK }; - if ((size_t)tto >= lengthof(tto_colour_remap)) return COLOUR_GREY; // this shouldn't happen + if (static_cast(tto) >= std::size(tto_colour_remap)) return COLOUR_GREY; // this shouldn't happen return tto_colour_remap[tto]; } diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index 67409c6449..0390cb0792 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -174,7 +174,7 @@ public: void Load(Town *t) const override { - size_t length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? (size_t)TAE_END : SlGetStructListLength(TAE_END); + size_t length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? static_cast(TAE_END) : SlGetStructListLength(TAE_END); for (size_t i = 0; i < length; i++) { SlObject(&t->received[i], this->GetLoadDescription()); } diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 8e3a684240..637d662125 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -241,7 +241,7 @@ ScriptObject::ActiveInstance::~ActiveInstance() /* static */ void ScriptObject::SetCallbackVariable(int index, int value) { - if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1); + if (static_cast(index) >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1); GetStorage()->callback_value[index] = value; } diff --git a/src/settings.cpp b/src/settings.cpp index 1dc89ef622..df6bcfba51 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -177,7 +177,7 @@ const uint16_t INIFILE_VERSION = (IniFileVersion)(IFV_MAX_VERSION - 1); ///< Cur * @param str the current value of the setting for which a value needs found * @param len length of the string * @param many full domain of values the ONEofMANY setting can have - * @return the integer index of the full-list, or -1 if not found + * @return the integer index of the full-list, or SIZE_MAX if not found */ size_t OneOfManySettingDesc::ParseSingleValue(const char *str, size_t len, const std::vector &many) { @@ -190,7 +190,7 @@ size_t OneOfManySettingDesc::ParseSingleValue(const char *str, size_t len, const idx++; } - return (size_t)-1; + return SIZE_MAX; } /** @@ -212,7 +212,7 @@ std::optional BoolSettingDesc::ParseSingleValue(const char *str) * @param many full domain of values the MANYofMANY setting can have * @param str the current string value of the setting, each individual * of separated by a whitespace,tab or | character - * @return the 'fully' set integer, or -1 if a set is not found + * @return the 'fully' set integer, or SIZE_MAX if a set is not found */ static size_t LookupManyOfMany(const std::vector &many, const char *str) { @@ -229,7 +229,7 @@ static size_t LookupManyOfMany(const std::vector &many, const char while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++; r = OneOfManySettingDesc::ParseSingleValue(str, s - str, many); - if (r == (size_t)-1) return r; + if (r == SIZE_MAX) return r; SetBit(res, (uint8_t)r); // value found, set it if (*s == 0) break; @@ -419,8 +419,8 @@ size_t OneOfManySettingDesc::ParseValue(const char *str) const size_t r = OneOfManySettingDesc::ParseSingleValue(str, strlen(str), this->many); /* if the first attempt of conversion from string to the appropriate value fails, * look if we have defined a converter from old value to new value. */ - if (r == (size_t)-1 && this->many_cnvt != nullptr) r = this->many_cnvt(str); - if (r != (size_t)-1) return r; // and here goes converted value + if (r == SIZE_MAX && this->many_cnvt != nullptr) r = this->many_cnvt(str); + if (r != SIZE_MAX) return r; // and here goes converted value ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE); msg.SetDParamStr(0, str); @@ -432,7 +432,7 @@ size_t OneOfManySettingDesc::ParseValue(const char *str) const size_t ManyOfManySettingDesc::ParseValue(const char *str) const { size_t r = LookupManyOfMany(this->many, str); - if (r != (size_t)-1) return r; + if (r != SIZE_MAX) return r; ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE); msg.SetDParamStr(0, str); msg.SetDParamStr(1, this->GetName()); diff --git a/src/sound.cpp b/src/sound.cpp index 02bdf82d04..7bb6d1ef26 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -117,7 +117,7 @@ static bool SetBankSource(MixerChannel *mc, const SoundEntry *sound) assert(sound != nullptr); /* Check for valid sound size. */ - if (sound->file_size == 0 || sound->file_size > ((size_t)-1) - 2) return false; + if (sound->file_size == 0 || sound->file_size > SIZE_MAX - 2) return false; int8_t *mem = MallocT(sound->file_size + 2); /* Add two extra bytes so rate conversion can read these diff --git a/src/stdafx.h b/src/stdafx.h index 3297c3eec2..41134921b6 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -370,7 +370,7 @@ inline void free(const void *ptr) * The largest value that can be entered in a variable * @param type the type of the variable */ -#define MAX_UVALUE(type) ((type)~(type)0) +#define MAX_UVALUE(type) (static_cast(~static_cast(0))) #if defined(_MSC_VER) && !defined(_DEBUG) # define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700)) diff --git a/src/string_func.h b/src/string_func.h index 6a2984e05d..2cbb4ef003 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -69,7 +69,7 @@ inline bool StrEmpty(const char *s) inline size_t ttd_strnlen(const char *str, size_t maxlen) { const char *t; - for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {} + for (t = str; static_cast(t - str) < maxlen && *t != '\0'; t++) {} return t - str; } diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index bb6c2d876d..5058def698 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -340,7 +340,7 @@ void TextfileWindow::CheckHyperlinkClick(Point pt) /* Found character index in line, check if any links are at that position. */ for (const auto &link : found_links) { Debug(misc, 4, "Checking link from char {} to {}", link.begin, link.end); - if ((size_t)char_index >= link.begin && (size_t)char_index < link.end) { + if (static_cast(char_index) >= link.begin && static_cast(char_index) < link.end) { Debug(misc, 4, "Activating link with destination: {}", link.destination); this->OnHyperlinkClick(link); return; diff --git a/src/widget_type.h b/src/widget_type.h index fad56477e3..f64ba8f7f5 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -845,7 +845,7 @@ public: template auto GetVisibleRangeIterators(Tcontainer &container) const { - assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match. + assert(static_cast(this->GetCount()) == container.size()); // Scrollbar and container size must match. auto first = std::next(std::begin(container), this->GetPosition()); auto last = std::next(first, std::min(this->GetCapacity(), this->GetCount() - this->GetPosition())); return std::make_pair(first, last); @@ -864,7 +864,7 @@ public: template typename Tcontainer::iterator GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const { - assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match. + assert(static_cast(this->GetCount()) == container.size()); // Scrollbar and container size must match. size_type row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height); if (row == Scrollbar::npos) return std::end(container);