Codechange: Replace C-style casts to size_t with static_cast. (#12455)

* Codechange: Replace C-style casts to size_t with static_cast.

This touches only simple value-type casts.

* Codechange: Replace static_cast<size_t>(-1) with SIZE_MAX

Co-authored-by: Rubidium <rubidium@openttd.org>
This commit is contained in:
Peter Nelson 2024-04-19 20:34:36 +01:00 committed by GitHub
parent 6ee31a2a22
commit a28ab8cac2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 38 additions and 38 deletions

View File

@ -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<Sprite *>(allocator(sizeof(*dest_sprite) + static_cast<size_t>(sprite[ZOOM_LVL_MIN].height) * static_cast<size_t>(sprite[ZOOM_LVL_MIN].width) * sizeof(*dst)));
dest_sprite->height = sprite[ZOOM_LVL_MIN].height;
dest_sprite->width = sprite[ZOOM_LVL_MIN].width;

View File

@ -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<Sprite *>(allocator(sizeof(*dest_sprite) + static_cast<size_t>(sprite[ZOOM_LVL_MIN].height) * static_cast<size_t>(sprite[ZOOM_LVL_MIN].width)));
dest_sprite->height = sprite[ZOOM_LVL_MIN].height;
dest_sprite->width = sprite[ZOOM_LVL_MIN].width;

View File

@ -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<size_t>(-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<size_t>(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<size_t>(IConsoleWindow::scroll + amount, max_scroll);
}
@ -223,7 +223,7 @@ struct IConsoleWindow : Window
/** Check on a regular interval if the console buffer needs truncating. */
IntervalTimer<TimerWindow> 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<size_t>(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;

View File

@ -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<size_t>(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<size_t>(*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<size_t>(*it) > index) return *position;
++position;
}
}

View File

@ -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<size_t>(end - start)};
for (const auto &kn : _keycode_to_name) {
if (StrEqualsIgnoreCase(str, kn.name)) {
return kn.keycode;

View File

@ -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<size_t>(dmax * dmax * 2)) {
const Industry *i = nullptr;
TileArea tile_area = TileArea(tile, 1, 1).Expand(dmax);
for (TileIndex atile : tile_area) {

View File

@ -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<size_t>(ftell(in)) < end && fgets(buffer, sizeof(buffer), in)) {
char c, *s;
/* trim whitespace from the left side */
for (s = buffer; *s == ' ' || *s == '\t'; s++) {}

View File

@ -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;

View File

@ -34,7 +34,7 @@ template <typename T, size_t N> struct ArrayT<T[N]> {
template <typename E, typename T>
inline typename ArrayT<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::Item t_unk)
{
if ((size_t)idx >= ArrayT<T>::length) {
if (static_cast<size_t>(idx) >= ArrayT<T>::length) {
return t_unk;
}
return t[idx];
@ -48,7 +48,7 @@ inline typename ArrayT<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::I
template <typename E, typename T>
inline typename ArrayT<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::Item t_unk, E idx_inv, typename ArrayT<T>::Item t_inv)
{
if ((size_t)idx < ArrayT<T>::length) {
if (static_cast<size_t>(idx) < ArrayT<T>::length) {
return t[idx];
}
if (idx == idx_inv) {

View File

@ -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<size_t>(this->buffer[0]);
size += static_cast<size_t>(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.

View File

@ -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<size_t>(nbytes) != p.Size()) {
Debug(net, 1, "Received a packet with mismatching size from {}", address.GetAddressAsString());
continue;
}

View File

@ -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<size_t>(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<size_t>(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();

View File

@ -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<size_t>(data[13]) << 24) | (static_cast<size_t>(data[12]) << 16) | (static_cast<size_t>(data[11]) << 8) | static_cast<size_t>(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

View File

@ -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;
}

View File

@ -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<size_t>(pos), SEEK_SET);
}
/**

View File

@ -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<size_t>(tto) >= std::size(tto_colour_remap)) return COLOUR_GREY; // this shouldn't happen
return tto_colour_remap[tto];
}

View File

@ -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<size_t>(TAE_END) : SlGetStructListLength(TAE_END);
for (size_t i = 0; i < length; i++) {
SlObject(&t->received[i], this->GetLoadDescription());
}

View File

@ -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<size_t>(index) >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
GetStorage()->callback_value[index] = value;
}

View File

@ -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<std::string> &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<bool> 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<std::string> &many, const char *str)
{
@ -229,7 +229,7 @@ static size_t LookupManyOfMany(const std::vector<std::string> &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());

View File

@ -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<int8_t>(sound->file_size + 2);
/* Add two extra bytes so rate conversion can read these

View File

@ -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<type>(~static_cast<type>(0)))
#if defined(_MSC_VER) && !defined(_DEBUG)
# define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))

View File

@ -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<size_t>(t - str) < maxlen && *t != '\0'; t++) {}
return t - str;
}

View File

@ -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<size_t>(char_index) >= link.begin && static_cast<size_t>(char_index) < link.end) {
Debug(misc, 4, "Activating link with destination: {}", link.destination);
this->OnHyperlinkClick(link);
return;

View File

@ -845,7 +845,7 @@ public:
template <typename Tcontainer>
auto GetVisibleRangeIterators(Tcontainer &container) const
{
assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match.
assert(static_cast<size_t>(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<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition()));
return std::make_pair(first, last);
@ -864,7 +864,7 @@ public:
template <typename Tcontainer>
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<size_t>(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);