From 57e2f367418e620f7709004c3325b6f74bacdb61 Mon Sep 17 00:00:00 2001 From: craftablescience Date: Sat, 24 Oct 2026 15:18:40 -0700 Subject: [PATCH] fix: take care of warnings + some skip overloads ignored template param --- CMakeLists.txt | 16 ++++++++-------- include/BufferStream.h | 10 +++++++--- include/FileStream.h | 17 +++++++++-------- test/BufferStream.cpp | 1 + 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b670495..eb12d1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Options -option(BUFFERSTREAM_BUILD_TESTS "Build tests" OFF) +option(BUFFERSTREAM_BUILD_TESTS "Build tests" ${PROJECT_IS_TOP_LEVEL}) # Create library add_library(${PROJECT_NAME} INTERFACE @@ -27,21 +27,21 @@ if(BUFFERSTREAM_BUILD_TESTS) FetchContent_MakeAvailable(googletest) enable_testing() - set(CMAKE_COMPILE_WARNING_AS_ERROR ON) - add_executable(${BUFFERSTREAM_TEST_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/test/BufferStream.cpp") target_link_libraries(${BUFFERSTREAM_TEST_NAME} PUBLIC gtest_main ${PROJECT_NAME}) - if (MSVC) - target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE - /W4) + # Enable warnings, warnings are errors + if(MSVC) + target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE /W4 /WX) else() - target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE - -Wall -Wextra -Wpedantic) + target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE -Wall -Wextra -Wpedantic) endif() + set_target_properties(${BUFFERSTREAM_TEST_NAME} PROPERTIES + COMPILE_WARNING_AS_ERROR ON + LINK_WARNING_AS_ERROR ON) include(GoogleTest) gtest_discover_tests(${BUFFERSTREAM_TEST_NAME}) diff --git a/include/BufferStream.h b/include/BufferStream.h index 096d3b5..bae03d4 100644 --- a/include/BufferStream.h +++ b/include/BufferStream.h @@ -1,3 +1,7 @@ +// ReSharper disable CppDiscardedPostfixOperatorResult +// ReSharper disable CppRedundantTypenameKeyword +// ReSharper disable CppUnusedIncludeDirective + #pragma once #include @@ -178,7 +182,7 @@ class BufferStream { template BufferStream& skip_u(std::uint64_t n = 1) { - return this->skip(static_cast(n)); + return this->skip(static_cast(n)); } [[nodiscard]] const std::byte* data() const { @@ -626,7 +630,7 @@ class BufferStream { BufferStream& write(std::string_view obj, bool addNullTerminator = true, std::uint64_t maxSize = 0) { static_assert(BufferStreamPODByteType, "String char width must be 1 byte!"); - bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0'; + const bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0'; if (maxSize == 0) { // Add true, bundled true - one null terminator // Add false, bundled true - null terminator removed @@ -672,7 +676,7 @@ class BufferStream { obj.reserve(n); for (std::uint64_t i = 0; i < n; i++) { - char temp = this->read(); + const auto temp = this->read(); if (temp == '\0' && stopOnNullTerminator) { // Read the required number of characters and exit this->skip_u(n - i - 1); diff --git a/include/FileStream.h b/include/FileStream.h index 4f296c7..43bb403 100644 --- a/include/FileStream.h +++ b/include/FileStream.h @@ -1,3 +1,5 @@ +// ReSharper disable CppRedundantTypenameKeyword + #pragma once #include @@ -24,8 +26,7 @@ class FileStream { explicit FileStream(const std::filesystem::path& path, int options = OPT_READ) : useExceptions(true) , bigEndian(false) { - std::error_code ec; - if (((options & OPT_CREATE_IF_NONEXISTENT) && !std::filesystem::exists(path, ec)) || ec) { + if (std::error_code ec; (options & OPT_CREATE_IF_NONEXISTENT && !std::filesystem::exists(path, ec)) || ec) { if (!std::filesystem::exists(path.parent_path(), ec)) { std::filesystem::create_directories(path.parent_path(), ec); } @@ -108,7 +109,7 @@ class FileStream { template FileStream& skip_in_u(std::uint64_t n = 1) { - return this->skip_in(static_cast(n)); + return this->skip_in(static_cast(n)); } template @@ -121,7 +122,7 @@ class FileStream { template FileStream& skip_out_u(std::uint64_t n = 1) { - return this->skip_out(static_cast(n)); + return this->skip_out(static_cast(n)); } [[nodiscard]] std::uint64_t tell_in() { @@ -330,7 +331,7 @@ class FileStream { }) { obj.reserve(n); } - for (int i = 0; i < n; i++) { + for (uint64_t i = 0; i < n; i++) { obj.push_back(this->read()); } } @@ -494,7 +495,7 @@ class FileStream { FileStream& write(std::string_view obj, bool addNullTerminator = true, std::uint64_t maxSize = 0) { static_assert(sizeof(typename std::string_view::value_type) == 1, "String char width must be 1 byte!"); - bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0'; + const bool bundledTerminator = !obj.empty() && obj[obj.size() - 1] == '\0'; if (maxSize == 0) { // Add true, bundled true - one null terminator // Add false, bundled true - null terminator removed @@ -531,8 +532,8 @@ class FileStream { } obj.reserve(n); - for (int i = 0; i < n; i++) { - char temp = this->read(); + for (uint64_t i = 0; i < n; i++) { + const auto temp = this->read(); if (temp == '\0' && stopOnNullTerminator) { // Read the required number of characters and exit this->skip_in_u(n - i - 1); diff --git a/test/BufferStream.cpp b/test/BufferStream.cpp index 9fa3a58..458287f 100644 --- a/test/BufferStream.cpp +++ b/test/BufferStream.cpp @@ -3,6 +3,7 @@ #include #include +#include struct POD { int x;