Skip to content

Commit 346a299

Browse files
[libc++][test] Fix unused and nodiscard warnings (#73437)
Found while running libc++'s test suite with MSVC's STL. This is structured into a series of commits for easier reviewing; I could also split this into smaller PRs if desired. * Add void-casts for `invoke_r` calls to fix MSVC STL `[[nodiscard]]` warnings. + Our rationale is that if someone is calling `invoke_r<NonVoidType>`, it sure looks like they care about the return value. * Add `[[maybe_unused]]` to silence `-Wunused-parameter` warnings. + This happens because the parameters are used within `LIBCPP_ASSERT`, which vanishes for MSVC's STL. This also motivates the following changes. * Add `[[maybe_unused]]` to fix `-Wunused-variable` warnings. * Always void-cast `debug_comparisons` to fix `-Wunused-variable` warnings. + As this was already unused with a void-cast in one `_LIBCPP_HARDENING_MODE` branch, I'm simply lifting it next to the variable definition. * Add `[[maybe_unused]]` to fix `-Wunused-local-typedef` warnings.
1 parent dc8b055 commit 346a299

File tree

20 files changed

+34
-40
lines changed

20 files changed

+34
-40
lines changed

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ int main(int, char**) {
6161
const int debug_elements = std::min(100, n);
6262
// Multiplier 2 because of comp(a,b) comp(b, a) checks.
6363
const int debug_comparisons = 2 * (debug_elements + 1) * debug_elements;
64+
(void)debug_comparisons;
6465
std::shuffle(first, last, g);
6566
std::make_heap(first, last);
6667
// The exact stats of our current implementation are recorded here.
@@ -70,7 +71,6 @@ int main(int, char**) {
7071
LIBCPP_ASSERT(stats.moved <= 2 * n + n * logn);
7172
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
7273
LIBCPP_ASSERT(stats.compared <= n * logn);
73-
(void)debug_comparisons;
7474
#else
7575
LIBCPP_ASSERT(stats.compared <= 2 * n * logn + debug_comparisons);
7676
#endif

libcxx/test/std/containers/sequences/deque/deque.cons/from_range.pass.cpp

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

2020
int main(int, char**) {
2121
for_all_iterators_and_allocators<int>([]<class Iter, class Sent, class Alloc>() {
22-
test_sequence_container<std::deque, int, Iter, Sent, Alloc>([](const auto& c) {
22+
test_sequence_container<std::deque, int, Iter, Sent, Alloc>([]([[maybe_unused]] const auto& c) {
2323
LIBCPP_ASSERT(c.__invariants());
2424
});
2525
});

libcxx/test/std/containers/sequences/deque/deque.modifiers/append_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int, char**) {
2626
static_assert(test_constraints_append_range<std::deque, int, double>());
2727

2828
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
29-
test_sequence_append_range<std::deque<int, Alloc>, Iter, Sent>([](auto&& c) {
29+
test_sequence_append_range<std::deque<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
3030
LIBCPP_ASSERT(c.__invariants());
3131
});
3232
});

libcxx/test/std/containers/sequences/deque/deque.modifiers/assign_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main(int, char**) {
2525
static_assert(test_constraints_assign_range<std::deque, int, double>());
2626

2727
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
28-
test_sequence_assign_range<std::deque<int, Alloc>, Iter, Sent>([](auto&& c) {
28+
test_sequence_assign_range<std::deque<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
2929
LIBCPP_ASSERT(c.__invariants());
3030
});
3131
});

libcxx/test/std/containers/sequences/deque/deque.modifiers/insert_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int, char**) {
2626
static_assert(test_constraints_insert_range<std::deque, int, double>());
2727

2828
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
29-
test_sequence_insert_range<std::deque<int, Alloc>, Iter, Sent>([](auto&& c) {
29+
test_sequence_insert_range<std::deque<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
3030
LIBCPP_ASSERT(c.__invariants());
3131
});
3232
});

libcxx/test/std/containers/sequences/deque/deque.modifiers/prepend_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int, char**) {
2626
static_assert(test_constraints_prepend_range<std::deque, int, double>());
2727

2828
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
29-
test_sequence_prepend_range<std::deque<int, Alloc>, Iter, Sent>([](auto&& c) {
29+
test_sequence_prepend_range<std::deque<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
3030
LIBCPP_ASSERT(c.__invariants());
3131
});
3232
});

libcxx/test/std/containers/sequences/vector.bool/append_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ constexpr bool test() {
2525
static_assert(test_constraints_append_range<std::vector, bool, char>());
2626

2727
for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() {
28-
test_sequence_append_range<std::vector<bool, Alloc>, Iter, Sent>([](auto&& c) {
28+
test_sequence_append_range<std::vector<bool, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
2929
LIBCPP_ASSERT(c.__invariants());
3030
// `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
3131
});

libcxx/test/std/containers/sequences/vector.bool/assign_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ constexpr bool test() {
2525
static_assert(test_constraints_assign_range<std::vector, bool, char>());
2626

2727
for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() {
28-
test_sequence_assign_range<std::vector<bool, Alloc>, Iter, Sent>([](auto&& c) {
28+
test_sequence_assign_range<std::vector<bool, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
2929
LIBCPP_ASSERT(c.__invariants());
3030
// `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
3131
});

libcxx/test/std/containers/sequences/vector.bool/construct_from_range.pass.cpp

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

1919
constexpr bool test() {
2020
for_all_iterators_and_allocators<bool>([]<class Iter, class Sent, class Alloc>() {
21-
test_vector_bool<Iter, Sent, Alloc>([](const auto& c) {
21+
test_vector_bool<Iter, Sent, Alloc>([]([[maybe_unused]] const auto& c) {
2222
LIBCPP_ASSERT(c.__invariants());
2323
// `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
2424
});

libcxx/test/std/containers/sequences/vector.bool/insert_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ constexpr bool test() {
2525
static_assert(test_constraints_insert_range<std::vector, bool, char>());
2626

2727
for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() {
28-
test_sequence_insert_range<std::vector<bool, Alloc>, Iter, Sent>([](auto&& c) {
28+
test_sequence_insert_range<std::vector<bool, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
2929
LIBCPP_ASSERT(c.__invariants());
3030
// `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
3131
});

libcxx/test/std/containers/sequences/vector/vector.cons/construct_from_range.pass.cpp

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

2020
constexpr bool test() {
2121
for_all_iterators_and_allocators<int>([]<class Iter, class Sent, class Alloc>() {
22-
test_sequence_container<std::vector, int, Iter, Sent, Alloc>([](const auto& c) {
22+
test_sequence_container<std::vector, int, Iter, Sent, Alloc>([]([[maybe_unused]] const auto& c) {
2323
LIBCPP_ASSERT(c.__invariants());
2424
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
2525
});

libcxx/test/std/containers/sequences/vector/vector.modifiers/append_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ constexpr bool test() {
2727
static_assert(test_constraints_append_range<std::vector, int, double>());
2828

2929
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
30-
test_sequence_append_range<std::vector<int, Alloc>, Iter, Sent>([](auto&& c) {
30+
test_sequence_append_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
3131
LIBCPP_ASSERT(c.__invariants());
3232
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
3333
});

libcxx/test/std/containers/sequences/vector/vector.modifiers/assign_range.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ constexpr bool test() {
2727
static_assert(test_constraints_assign_range<std::vector, int, double>());
2828

2929
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
30-
test_sequence_assign_range<std::vector<int, Alloc>, Iter, Sent>([](auto&& c) {
30+
test_sequence_assign_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
3131
LIBCPP_ASSERT(c.__invariants());
3232
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
3333
});

libcxx/test/std/containers/sequences/vector/vector.modifiers/insert_range.pass.cpp

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

2727
constexpr bool test() {
2828
for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
29-
test_sequence_insert_range<std::vector<int, Alloc>, Iter, Sent>([](auto&& c) {
29+
test_sequence_insert_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
3030
LIBCPP_ASSERT(c.__invariants());
3131
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
3232
});

libcxx/test/std/strings/basic.string/string.modifiers/string_append/append_range.pass.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323

2424
constexpr bool test_constexpr() {
2525
for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
26-
test_sequence_append_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
27-
LIBCPP_ASSERT(c.__invariants());
28-
});
26+
test_sequence_append_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
27+
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
2928
});
3029

3130
return true;
@@ -35,9 +34,8 @@ int main(int, char**) {
3534
static_assert(test_constraints_append_range<std::basic_string, char, int>());
3635

3736
for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
38-
test_sequence_append_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
39-
LIBCPP_ASSERT(c.__invariants());
40-
});
37+
test_sequence_append_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
38+
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
4139
});
4240
static_assert(test_constexpr());
4341

libcxx/test/std/strings/basic.string/string.modifiers/string_assign/assign_range.pass.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323

2424
constexpr bool test_constexpr() {
2525
for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
26-
test_sequence_assign_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
27-
LIBCPP_ASSERT(c.__invariants());
28-
});
26+
test_sequence_assign_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
27+
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
2928
});
3029

3130
return true;
@@ -35,9 +34,8 @@ int main(int, char**) {
3534
static_assert(test_constraints_assign_range<std::basic_string, char, int>());
3635

3736
for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
38-
test_sequence_assign_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
39-
LIBCPP_ASSERT(c.__invariants());
40-
});
37+
test_sequence_assign_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
38+
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
4139
});
4240
static_assert(test_constexpr());
4341

libcxx/test/std/strings/basic.string/string.modifiers/string_insert/insert_range.pass.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323

2424
constexpr bool test_constexpr() {
2525
for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
26-
test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
27-
LIBCPP_ASSERT(c.__invariants());
28-
});
26+
test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
27+
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
2928
});
3029

3130
return true;
@@ -35,9 +34,8 @@ int main(int, char**) {
3534
static_assert(test_constraints_insert_range<std::basic_string, char, int>());
3635

3736
for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
38-
test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
39-
LIBCPP_ASSERT(c.__invariants());
40-
});
37+
test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
38+
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
4139
});
4240
static_assert(test_constexpr());
4341

libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@ int main(int, char**) {
3838
#if TEST_STD_VER >= 17
3939
{
4040
std::future_error const f(std::future_errc::broken_promise);
41-
char const* what = f.what();
41+
[[maybe_unused]] char const* what = f.what();
4242
LIBCPP_ASSERT(what == std::string_view{"The associated promise has been destructed prior "
4343
"to the associated state becoming ready."});
4444
}
4545
{
4646
std::future_error f(std::future_errc::future_already_retrieved);
47-
char const* what = f.what();
47+
[[maybe_unused]] char const* what = f.what();
4848
LIBCPP_ASSERT(what == std::string_view{"The future has already been retrieved from "
4949
"the promise or packaged_task."});
5050
}
5151
{
5252
std::future_error f(std::future_errc::promise_already_satisfied);
53-
char const* what = f.what();
53+
[[maybe_unused]] char const* what = f.what();
5454
LIBCPP_ASSERT(what == std::string_view{"The state of the promise has already been set."});
5555
}
5656
{
5757
std::future_error f(std::future_errc::no_state);
58-
char const* what = f.what();
58+
[[maybe_unused]] char const* what = f.what();
5959
LIBCPP_ASSERT(what == std::string_view{"Operation not permitted on an object without "
6060
"an associated state."});
6161
}

libcxx/test/std/utilities/function.objects/func.invoke/invoke_r.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ constexpr bool test() {
9191
{
9292
bool was_called = false;
9393
auto f = [&](NonCopyable) -> int { was_called = true; return 0; };
94-
std::invoke_r<int>(f, NonCopyable());
94+
(void)std::invoke_r<int>(f, NonCopyable());
9595
assert(was_called);
9696
}
9797
// Forward function object, with void return
@@ -111,7 +111,7 @@ constexpr bool test() {
111111
constexpr int operator()() && { was_called = true; return 0; }
112112
};
113113
bool was_called = false;
114-
std::invoke_r<int>(MoveOnlyIntFunction{was_called});
114+
(void)std::invoke_r<int>(MoveOnlyIntFunction{was_called});
115115
assert(was_called);
116116
}
117117
}

libcxx/test/support/deduction_guides_sfinae_checks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ constexpr void SequenceContainerDeductionGuidesSfinaeAway() {
116116
template<template<typename ...> class Container, typename InstantiatedContainer>
117117
constexpr void ContainerAdaptorDeductionGuidesSfinaeAway() {
118118
using T = typename InstantiatedContainer::value_type;
119-
using Alloc = std::allocator<T>;
119+
using Alloc [[maybe_unused]] = std::allocator<T>;
120120
using Iter = T*;
121121

122-
using BadIter = int;
122+
using BadIter [[maybe_unused]] = int;
123123
using BadAlloc = Empty;
124124

125125
// (container) -- no constraints.

0 commit comments

Comments
 (0)