Skip to content

Commit 8bb3618

Browse files
committed
AK: Remove Optional::operator bool()
This was causing some obvious-in-hindsight but hard to spot bugs where we'd implicitly convert the bool to an integer type and carry on with the number 1 instead of the actual value().
1 parent ae233c1 commit 8bb3618

File tree

6 files changed

+9
-12
lines changed

6 files changed

+9
-12
lines changed

AK/Bitmap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ class Bitmap {
165165
if (!first_index.has_value())
166166
return {};
167167

168-
size_t free_region_start = first_index;
168+
size_t free_region_start = first_index.value();
169169
size_t free_region_size = 1;
170170

171171
size_t max_region_start = free_region_start;
172172
size_t max_region_size = free_region_size;
173173

174174
// Let's try and find the best fit possible
175-
for (size_t j = first_index + 1; j < m_size && free_region_size < max_length; j++) {
175+
for (size_t j = first_index.value() + 1; j < m_size && free_region_size < max_length; j++) {
176176
if (!get(j)) {
177177
if (free_region_size == 0)
178178
free_region_start = j;

AK/Optional.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ class CONSUMABLE(unknown) alignas(T) Optional {
150150
return fallback;
151151
}
152152

153-
SET_TYPESTATE(consumed)
154-
operator bool() const { return m_has_value; }
155-
156153
private:
157154
// Call when we don't want to alter the consume state
158155
const T& value_without_consume_state() const

Kernel/FileSystem/Ext2FileSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,9 @@ Ext2FS::BlockIndex Ext2FS::allocate_block(GroupIndex preferred_group_index)
10761076
auto block_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), blocks_in_group);
10771077

10781078
BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + first_block_index();
1079-
int first_unset_bit_index = block_bitmap.find_first_unset();
1080-
ASSERT(first_unset_bit_index != -1);
1081-
BlockIndex block_index = (unsigned)first_unset_bit_index + first_block_in_group;
1079+
auto first_unset_bit_index = block_bitmap.find_first_unset();
1080+
ASSERT(first_unset_bit_index.has_value());
1081+
BlockIndex block_index = first_unset_bit_index.value() + first_block_in_group;
10821082
set_block_allocation_state(block_index, true);
10831083
return block_index;
10841084
}

Kernel/FileSystem/ProcFS.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ ssize_t ProcFSInode::read_bytes(off_t offset, ssize_t count, u8* buffer, FileDes
12281228
if (!description) {
12291229
generated_data = (*read_callback)(identifier());
12301230
} else {
1231-
if (!description->generator_cache())
1231+
if (!description->generator_cache().has_value())
12321232
description->generator_cache() = (*read_callback)(identifier());
12331233
generated_data = description->generator_cache();
12341234
}
@@ -1242,7 +1242,7 @@ ssize_t ProcFSInode::read_bytes(off_t offset, ssize_t count, u8* buffer, FileDes
12421242

12431243
ssize_t nread = min(static_cast<off_t>(data.value().size() - offset), static_cast<off_t>(count));
12441244
memcpy(buffer, data.value().data() + offset, nread);
1245-
if (nread == 0 && description && description->generator_cache())
1245+
if (nread == 0 && description && description->generator_cache().has_value())
12461246
description->generator_cache().clear();
12471247

12481248
return nread;

Libraries/LibHTML/DOM/Element.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ NonnullRefPtr<StyleProperties> Element::computed_style()
209209
};
210210
for (CSS::PropertyID id : box_model_metrics) {
211211
auto prop = layout_node()->style().property(id);
212-
if (prop)
212+
if (prop.has_value())
213213
properties->set_property(id, prop.value());
214214
}
215215
}

Servers/LookupServer/LookupServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void LookupServer::service_client(RefPtr<Core::LocalSocket> socket)
117117

118118
Vector<String> responses;
119119

120-
if (auto known_host = m_etc_hosts.get(hostname)) {
120+
if (auto known_host = m_etc_hosts.get(hostname); known_host.has_value()) {
121121
responses.append(known_host.value());
122122
} else if (!hostname.is_empty()) {
123123
bool did_timeout;

0 commit comments

Comments
 (0)