fix: bugprone-narrowing-conversions surrounding error codes (#8716)

This commit is contained in:
Yat Ho
2026-03-23 22:12:38 +08:00
committed by GitHub
parent 0960c4e3c3
commit dba3e8f9d3
8 changed files with 33 additions and 20 deletions

View File

@@ -6,6 +6,7 @@
#pragma once
#ifdef _WIN32
#include <cstdint>
#include <windows.h>
@@ -17,6 +18,14 @@
#define TR_ERROR_EINVAL ERROR_INVALID_PARAMETER
#define TR_ERROR_EISDIR ERROR_DIRECTORY_NOT_SUPPORTED
// Smallest integer type that can represent all values returned
// by `DWORD GetLastError()` and `int WSAGetLastError()`.
//
// N.B. DWORD is an unsigned 32-bit integer, so int64_t is the next
// smallest signed integer type that can represent all its values.
using tr_error_code_t = int64_t;
static_assert(sizeof(int) <= sizeof(int64_t));
#else /* _WIN32 */
#include <cerrno>
@@ -24,9 +33,11 @@
#define TR_ERROR_EINVAL EINVAL
#define TR_ERROR_EISDIR EISDIR
using tr_error_code_t = int;
#endif /* _WIN32 */
constexpr bool tr_error_is_enoent(int code) noexcept
constexpr bool tr_error_is_enoent(tr_error_code_t code) noexcept
{
#ifdef _WIN32
return code == ERROR_FILE_NOT_FOUND || code == ERROR_PATH_NOT_FOUND;
@@ -35,7 +46,7 @@ constexpr bool tr_error_is_enoent(int code) noexcept
#endif
}
constexpr bool tr_error_is_enospc(int code) noexcept
constexpr bool tr_error_is_enospc(tr_error_code_t code) noexcept
{
#ifdef _WIN32
return code == ERROR_DISK_FULL;