Skip to content

Commit 765936e

Browse files
authored
Everywhere: Switch from (void) to [[maybe_unused]] (#4473)
Problem: - `(void)` simply casts the expression to void. This is understood to indicate that it is ignored, but this is really a compiler trick to get the compiler to not generate a warning. Solution: - Use the `[[maybe_unused]]` attribute to indicate the value is unused. Note: - Functions taking a `(void)` argument list have also been changed to `()` because this is not needed and shows up in the same grep command.
1 parent 4421d98 commit 765936e

File tree

103 files changed

+219
-362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+219
-362
lines changed

AK/Random.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,13 @@
4343

4444
namespace AK {
4545

46-
inline void fill_with_random(void* buffer, size_t length)
46+
inline void fill_with_random([[maybe_unused]] void* buffer, [[maybe_unused]] size_t length)
4747
{
4848
#if defined(__serenity__)
4949
arc4random_buf(buffer, length);
5050
#elif defined(OSS_FUZZ)
51-
(void)buffer;
52-
(void)length;
5351
#elif defined(__unix__) or defined(__APPLE__)
54-
int rc = getentropy(buffer, length);
55-
(void)rc;
52+
[[maybe_unused]] int rc = getentropy(buffer, length);
5653
#endif
5754
}
5855

AK/SharedBuffer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,14 @@ RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
8989
return adopt(*new SharedBuffer(shbuf_id, size, data));
9090
}
9191

92-
bool SharedBuffer::share_with(pid_t peer)
92+
bool SharedBuffer::share_with([[maybe_unused]] pid_t peer)
9393
{
9494
# if defined(__serenity__)
9595
int ret = shbuf_allow_pid(shbuf_id(), peer);
9696
if (ret < 0) {
9797
perror("shbuf_allow_pid");
9898
return false;
9999
}
100-
# else
101-
(void)peer;
102100
# endif
103101
return true;
104102
}

AK/Singleton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Singleton {
114114

115115
void ensure_instance()
116116
{
117-
(void)ptr();
117+
ptr();
118118
}
119119

120120
private:

AK/StdLibExtras.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
#pragma once
2828

29-
#define UNUSED_PARAM(x) (void)x
30-
3129
constexpr unsigned round_up_to_power_of_two(unsigned value, unsigned power_of_two)
3230
{
3331
return ((value - 1) & ~(power_of_two - 1)) + power_of_two;

AK/Tests/TestByteBuffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TEST_CASE(negative_operator_lt)
6565
{
6666
ByteBuffer a = ByteBuffer::copy("Hello, world", 10);
6767
ByteBuffer b = ByteBuffer::copy("Hello, friend", 10);
68-
(void)(a < b);
68+
[[maybe_unused]] auto res = a < b;
6969
// error: error: use of deleted function ‘bool AK::ByteBuffer::operator<(const AK::ByteBuffer&) const’
7070
}
7171
#endif /* COMPILE_NEGATIVE_TESTS */

AK/Tests/TestDistinctNumeric.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,35 +264,35 @@ TEST_CASE(negative_incr)
264264
TEST_CASE(negative_cmp)
265265
{
266266
BareNumeric a = 12;
267-
(void)(a < a);
267+
[[maybe_unused]] auto res = (a < a);
268268
// error: static assertion failed: 'a<b' is only available for DistinctNumeric types with 'Cmp'.
269269
}
270270

271271
TEST_CASE(negative_bool)
272272
{
273273
BareNumeric a = 12;
274-
(void)!a;
274+
[[maybe_unused]] auto res = !a;
275275
// error: static assertion failed: '!a', 'a&&b', 'a||b' and similar operators are only available for DistinctNumeric types with 'Bool'.
276276
}
277277

278278
TEST_CASE(negative_flags)
279279
{
280280
BareNumeric a = 12;
281-
(void)(a & a);
281+
[[maybe_unused]] auto res = (a & a);
282282
// error: static assertion failed: 'a&b' is only available for DistinctNumeric types with 'Flags'.
283283
}
284284

285285
TEST_CASE(negative_shift)
286286
{
287287
BareNumeric a = 12;
288-
(void)(a << a);
288+
[[maybe_unused]] auto res = (a << a);
289289
// error: static assertion failed: 'a<<b' is only available for DistinctNumeric types with 'Shift'.
290290
}
291291

292292
TEST_CASE(negative_arith)
293293
{
294294
BareNumeric a = 12;
295-
(void)(a + a);
295+
[[maybe_unused]] auto res = (a + a);
296296
// error: static assertion failed: 'a+b' is only available for DistinctNumeric types with 'Arith'.
297297
}
298298

@@ -302,13 +302,13 @@ TEST_CASE(negative_incompatible)
302302
ArithNumeric b = 345;
303303
// And this is the entire point of `DistinctNumeric`:
304304
// Theoretically, the operation *could* be supported, but we declared those int types incompatible.
305-
(void)(a + b);
305+
[[maybe_unused]] auto res = (a + b);
306306
// error: no match for ‘operator+’ (operand types are ‘GeneralNumeric’ {aka ‘AK::DistinctNumeric<int, true, true, true, true, true, true, 64, 64>’} and ‘ArithNumeric’ {aka ‘AK::DistinctNumeric<int, false, false, false, false, false, true, 64, 63>’})
307-
// 313 | (void)(a + b);
308-
// | ~ ^ ~
309-
// | | |
310-
// | | DistinctNumeric<[...],false,false,false,false,false,[...],[...],63>
311-
// | DistinctNumeric<[...],true,true,true,true,true,[...],[...],64>
307+
// 313 | [[maybe_unused]] auto res = (a + b);
308+
// | ~ ^ ~
309+
// | | |
310+
// | | DistinctNumeric<[...],false,false,false,false,false,[...],[...],63>
311+
// | DistinctNumeric<[...],true,true,true,true,true,[...],[...],64>
312312
}
313313
#endif /* COMPILE_NEGATIVE_TESTS */
314314

AK/Tests/TestJSON.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ TEST_CASE(load_form)
6161
widgets.for_each([&](const JsonValue& widget_value) {
6262
auto& widget_object = widget_value.as_object();
6363
auto widget_class = widget_object.get("class").as_string();
64-
widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
65-
(void)property_name;
66-
(void)property_value;
64+
widget_object.for_each_member([&]([[maybe_unused]] auto& property_name, [[maybe_unused]] const JsonValue& property_value) {
6765
//dbgprintf("Set property %s.%s to '%s'\n", widget_class.characters(), property_name.characters(), property_value.serialized().characters());
6866
});
6967
});

AK/Tests/TestSourceGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ TEST_CASE(generate_c_code)
4444
SourceGenerator generator { builder };
4545
generator.set("name", "foo");
4646

47-
generator.append("const char* @name@ (void) { return \"@name@\"; }");
47+
generator.append("const char* @name@ () { return \"@name@\"; }");
4848

49-
EXPECT_EQ(generator.as_string_view(), "const char* foo (void) { return \"foo\"; }");
49+
EXPECT_EQ(generator.as_string_view(), "const char* foo () { return \"foo\"; }");
5050
}
5151

5252
TEST_CASE(scoped)

AK/Utf8View.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ bool Utf8View::validate(size_t& valid_bytes) const
138138
size_t Utf8View::calculate_length() const
139139
{
140140
size_t length = 0;
141-
for (auto code_point : *this) {
142-
(void)code_point;
141+
for ([[maybe_unused]] auto code_point : *this) {
143142
++length;
144143
}
145144
return length;
@@ -170,7 +169,6 @@ Utf8CodepointIterator& Utf8CodepointIterator::operator++()
170169
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
171170

172171
ASSERT(first_byte_makes_sense);
173-
(void)value;
174172

175173
ASSERT(code_point_length_in_bytes <= m_length);
176174
m_ptr += code_point_length_in_bytes;

Applications/Browser/DownloadWidget.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ void DownloadWidget::did_progress(Optional<u32> total_size, u32 downloaded_size)
156156
}
157157
}
158158

159-
void DownloadWidget::did_finish(bool success, ReadonlyBytes payload, RefPtr<SharedBuffer> payload_storage, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)
159+
void DownloadWidget::did_finish(bool success, [[maybe_unused]] ReadonlyBytes payload, [[maybe_unused]] RefPtr<SharedBuffer> payload_storage, [[maybe_unused]] const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)
160160
{
161-
(void)payload;
162-
(void)payload_storage;
163-
(void)response_headers;
164161
dbg() << "did_finish, success=" << success;
165162

166163
m_close_button->set_enabled(true);

0 commit comments

Comments
 (0)