Fixes to compile cleanly under MSVC warning-level 4 and /analyze. · AIENGINE/cppcoro@f134929 · GitHub
Skip to content

Commit f134929

Browse files
committed
Fixes to compile cleanly under MSVC warning-level 4 and /analyze.
- Add [[maybe_unused]] to various parameters/variables. - Rework a few loops in test-cases to get around some warnings about unreachable code.
1 parent 1140628 commit f134929

12 files changed

Lines changed: 34 additions & 17 deletions

config.cake

Lines changed: 4 additions & 1 deletion

include/cppcoro/config.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
# define CPPCORO_COMPILER_GCC 0
3131
#endif
3232

33+
#if CPPCORO_COMPILER_MSVC
34+
# define CPPCORO_ASSUME(X) __assume(X)
35+
#else
36+
# define CPPCORO_ASSUME(X)
37+
#endif
38+
3339
/////////////////////////////////////////////////////////////////////////////
3440
// OS Detection
3541

include/cppcoro/lazy_task.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace cppcoro
4545

4646
bool await_ready() const noexcept { return false; }
4747

48-
void await_suspend(std::experimental::coroutine_handle<> coroutine)
48+
void await_suspend([[maybe_unused]] std::experimental::coroutine_handle<> coroutine)
4949
{
5050
m_awaiter.resume();
5151
}

include/cppcoro/operation_cancelled.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace cppcoro
1111
{
12-
class operation_cancelled : std::exception
12+
class operation_cancelled : public std::exception
1313
{
1414
public:
1515

lib/async_mutex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cppcoro::async_mutex::async_mutex() noexcept
1414

1515
cppcoro::async_mutex::~async_mutex()
1616
{
17-
auto state = m_state.load(std::memory_order_relaxed);
17+
[[maybe_unused]] auto state = m_state.load(std::memory_order_relaxed);
1818
assert(state == not_locked || state == locked_no_waiters);
1919
assert(m_waiters == nullptr);
2020
}

lib/cancellation_state.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "cancellation_state.hpp"
77

8+
#include "cppcoro/config.hpp"
9+
810
#include <cppcoro/cancellation_registration.hpp>
911

1012
#include <cassert>
@@ -214,6 +216,11 @@ cppcoro::detail::cancellation_registration_state::add_registration(
214216
lastChunk = next;
215217
}
216218

219+
// Work around false-warning raised by MSVC static analysis complaining that
220+
// warning C28182: Dereferencing NULL pointer. 'lastChunk' contains the same NULL value as 'chunk' did.
221+
// on statement initialising 'elementCount' below.
222+
CPPCORO_ASSUME(lastChunk != nullptr);
223+
217224
if (lastChunk != originalLastChunk)
218225
{
219226
// Update the cache of last chunk pointer so that subsequent

lib/file_read_operation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void cppcoro::file_read_operation::on_operation_completed(
262262
detail::win32::io_state* ioState,
263263
detail::win32::dword_t errorCode,
264264
detail::win32::dword_t numberOfBytesTransferred,
265-
detail::win32::ulongptr_t completionKey) noexcept
265+
[[maybe_unused]] detail::win32::ulongptr_t completionKey) noexcept
266266
{
267267
auto* operation = static_cast<file_read_operation*>(ioState);
268268

lib/file_write_operation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void cppcoro::file_write_operation::on_operation_completed(
262262
detail::win32::io_state* ioState,
263263
detail::win32::dword_t errorCode,
264264
detail::win32::dword_t numberOfBytesTransferred,
265-
detail::win32::ulongptr_t completionKey) noexcept
265+
[[maybe_unused]] detail::win32::ulongptr_t completionKey) noexcept
266266
{
267267
auto* operation = static_cast<file_write_operation*>(ioState);
268268

test/async_generator_tests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ TEST_CASE("default-constructed async_generator is an empty sequence")
1616
// Iterating over default-constructed async_generator just
1717
// gives an empty sequence.
1818
cppcoro::async_generator<int> g;
19-
for co_await(int x : g)
20-
{
21-
CHECK(false);
22-
}
19+
auto it = co_await g.begin();
20+
CHECK(it == g.end());
2321
}();
2422
}
2523

test/doctest/doctest.h

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)