diff options
author | Lenny Maiorani <lenny@colorado.edu> | 2021-01-10 16:29:28 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-12 09:11:45 +0100 |
commit | e6f907a1556757c623fe660df0a43faf1b3d0eae (patch) | |
tree | a3a07081ec2ebdac050d776c61d2c908459bf343 /Libraries | |
parent | 9dc44bf8c427160bafd87205c42201cf9b11c0b4 (diff) | |
download | serenity-e6f907a1556757c623fe660df0a43faf1b3d0eae.zip |
AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
instead of requiring the caller to default construct. This violates
the C++ Core Guidelines suggestion to declare single-argument
constructors explicit
(https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).
Solution:
- Change default constructors to use the compiler-provided default
constructor.
- Remove implicit conversion operators from `nullptr_t` and change
usage to enforce type consistency without conversion.
Diffstat (limited to 'Libraries')
26 files changed, 56 insertions, 54 deletions
diff --git a/Libraries/LibCore/Event.cpp b/Libraries/LibCore/Event.cpp index e7de1886b4..fac44e5537 100644 --- a/Libraries/LibCore/Event.cpp +++ b/Libraries/LibCore/Event.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/WeakPtr.h> #include <LibCore/Event.h> #include <LibCore/Object.h> @@ -32,7 +33,7 @@ namespace Core { ChildEvent::ChildEvent(Type type, Object& child, Object* insertion_before_child) : Core::Event(type) , m_child(child.make_weak_ptr()) - , m_insertion_before_child(insertion_before_child ? insertion_before_child->make_weak_ptr() : nullptr) + , m_insertion_before_child(AK::try_make_weak_ptr(insertion_before_child)) { } diff --git a/Libraries/LibCore/EventLoop.cpp b/Libraries/LibCore/EventLoop.cpp index 9fe4a2039e..c13cc986ea 100644 --- a/Libraries/LibCore/EventLoop.cpp +++ b/Libraries/LibCore/EventLoop.cpp @@ -503,7 +503,7 @@ bool SignalHandlers::remove(int handler_id) auto it = m_handlers.find(handler_id); if (it != m_handlers.end()) { // Mark pending remove - m_handlers_pending.set(handler_id, nullptr); + m_handlers_pending.set(handler_id, {}); return true; } it = m_handlers_pending.find(handler_id); diff --git a/Libraries/LibCore/Object.h b/Libraries/LibCore/Object.h index ef568f84d5..9270d347d0 100644 --- a/Libraries/LibCore/Object.h +++ b/Libraries/LibCore/Object.h @@ -256,7 +256,7 @@ const LogStream& operator<<(const LogStream&, const Object&); register_property( \ property_name, \ [this] { return this->getter(); }, \ - nullptr); + {}); #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \ register_property( \ diff --git a/Libraries/LibCoreDump/Reader.cpp b/Libraries/LibCoreDump/Reader.cpp index da7eeabe85..e12611bd46 100644 --- a/Libraries/LibCoreDump/Reader.cpp +++ b/Libraries/LibCoreDump/Reader.cpp @@ -37,7 +37,7 @@ OwnPtr<Reader> Reader::create(const String& path) { auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) - return nullptr; + return {}; return adopt_own(*new Reader(file_or_error.release_value())); } diff --git a/Libraries/LibDebug/DebugSession.cpp b/Libraries/LibDebug/DebugSession.cpp index d231d243e3..82871d070c 100644 --- a/Libraries/LibDebug/DebugSession.cpp +++ b/Libraries/LibDebug/DebugSession.cpp @@ -90,12 +90,12 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String if (waitpid(pid, nullptr, WSTOPPED) != pid) { perror("waitpid"); - return nullptr; + return {}; } if (ptrace(PT_ATTACH, pid, 0, 0) < 0) { perror("PT_ATTACH"); - return nullptr; + return {}; } // We want to continue until the exit from the 'execve' sycsall. @@ -105,7 +105,7 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String if (waitpid(pid, nullptr, WSTOPPED) != pid) { perror("wait_pid"); - return nullptr; + return {}; } auto debug_session = adopt_own(*new DebugSession(pid, source_root)); @@ -114,7 +114,7 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String int wstatus = debug_session->continue_debuggee_and_wait(); if (WSTOPSIG(wstatus) != SIGTRAP) { dbgln("expected SIGTRAP"); - return nullptr; + return {}; } // At this point, libraries should have been loaded diff --git a/Libraries/LibGUI/Action.cpp b/Libraries/LibGUI/Action.cpp index 5a9743df7e..208b7b5c90 100644 --- a/Libraries/LibGUI/Action.cpp +++ b/Libraries/LibGUI/Action.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/WeakPtr.h> #include <LibGUI/AboutDialog.h> #include <LibGUI/Action.h> #include <LibGUI/ActionGroup.h> @@ -39,7 +40,7 @@ namespace CommonActions { NonnullRefPtr<Action> make_about_action(const String& app_name, const Icon& app_icon, Window* parent) { - WeakPtr<Window> weak_parent = parent ? parent->make_weak_ptr<Window>() : nullptr; + auto weak_parent = AK::try_make_weak_ptr<Window>(parent); return Action::create(String::formatted("About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) { AboutDialog::show(app_name, app_icon.bitmap_for_size(32), weak_parent.ptr()); }); @@ -288,7 +289,7 @@ void Action::set_checked(bool checked) void Action::set_group(Badge<ActionGroup>, ActionGroup* group) { - m_action_group = group ? group->make_weak_ptr() : nullptr; + m_action_group = AK::try_make_weak_ptr(group); } void Action::set_icon(const Gfx::Bitmap* icon) diff --git a/Libraries/LibIPC/Connection.h b/Libraries/LibIPC/Connection.h index 3ee01a95a5..6ceba10ea3 100644 --- a/Libraries/LibIPC/Connection.h +++ b/Libraries/LibIPC/Connection.h @@ -171,7 +171,7 @@ protected: if (!drain_messages_from_peer()) break; } - return nullptr; + return {}; } bool drain_messages_from_peer() diff --git a/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp b/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp index 4a2ae30351..846acbd562 100644 --- a/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp @@ -43,7 +43,7 @@ void ArrayBufferPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.slice, slice, 2, attr); // FIXME: This should be an accessor property - define_native_property(vm.names.byteLength, byte_length_getter, nullptr, Attribute::Configurable); + define_native_property(vm.names.byteLength, byte_length_getter, {}, Attribute::Configurable); define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "ArrayBuffer"), Attribute::Configurable); } diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.cpp b/Libraries/LibJS/Runtime/ErrorPrototype.cpp index 29dfb72650..68affb5745 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ErrorPrototype.cpp @@ -45,7 +45,7 @@ void ErrorPrototype::initialize(GlobalObject& global_object) Object::initialize(global_object); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_property(vm.names.name, name_getter, name_setter, attr); - define_native_property(vm.names.message, message_getter, nullptr, attr); + define_native_property(vm.names.message, message_getter, {}, attr); define_native_function(vm.names.toString, to_string, 0, attr); } diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index b3c13ce41c..8540b7bc06 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -50,11 +50,11 @@ void RegExpPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.exec, exec, 1, attr); u8 readable_attr = Attribute::Configurable; - define_native_property(vm.names.flags, flags, nullptr, readable_attr); - define_native_property(vm.names.source, source, nullptr, readable_attr); + define_native_property(vm.names.flags, flags, {}, readable_attr); + define_native_property(vm.names.source, source, {}, readable_attr); #define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \ - define_native_property(vm.names.flagName, flag_name, nullptr, readable_attr); + define_native_property(vm.names.flagName, flag_name, {}, readable_attr); JS_ENUMERATE_REGEXP_FLAGS #undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index c5fef18602..56ce94edd2 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -73,8 +73,8 @@ void ScriptFunction::initialize(GlobalObject& global_object) prototype->define_property(vm.names.constructor, this, Attribute::Writable | Attribute::Configurable); define_property(vm.names.prototype, prototype, Attribute::Writable); } - define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable); - define_native_property(vm.names.name, name_getter, nullptr, Attribute::Configurable); + define_native_property(vm.names.length, length_getter, {}, Attribute::Configurable); + define_native_property(vm.names.name, name_getter, {}, Attribute::Configurable); } ScriptFunction::~ScriptFunction() diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 640a962662..d0f4d495ff 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -82,7 +82,7 @@ void StringPrototype::initialize(GlobalObject& global_object) StringObject::initialize(global_object); u8 attr = Attribute::Writable | Attribute::Configurable; - define_native_property(vm.names.length, length_getter, nullptr, 0); + define_native_property(vm.names.length, length_getter, {}, 0); define_native_function(vm.names.charAt, char_at, 1, attr); define_native_function(vm.names.charCodeAt, char_code_at, 1, attr); define_native_function(vm.names.repeat, repeat, 1, attr); diff --git a/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Libraries/LibJS/Runtime/SymbolPrototype.cpp index c6af026829..0a36ab4a20 100644 --- a/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -47,7 +47,7 @@ void SymbolPrototype::initialize(GlobalObject& global_object) { auto& vm = this->vm(); Object::initialize(global_object); - define_native_property(vm.names.description, description_getter, nullptr, Attribute::Configurable); + define_native_property(vm.names.description, description_getter, {}, Attribute::Configurable); define_native_function(vm.names.toString, to_string, 0, Attribute::Writable | Attribute::Configurable); define_native_function(vm.names.valueOf, value_of, 0, Attribute::Writable | Attribute::Configurable); diff --git a/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index f1f3ff84d2..da0c43faf1 100644 --- a/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -40,7 +40,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object) auto& vm = this->vm(); Object::initialize(object); // FIXME: This should be an accessor property - define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable); + define_native_property(vm.names.length, length_getter, {}, Attribute::Configurable); } TypedArrayPrototype::~TypedArrayPrototype() diff --git a/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp b/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp index aa5bcbf36c..c558f41119 100644 --- a/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp +++ b/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp @@ -42,7 +42,7 @@ Uint8ClampedArray::Uint8ClampedArray(u32 length, Object& prototype) , m_length(length) { auto& vm = this->vm(); - define_native_property(vm.names.length, length_getter, nullptr); + define_native_property(vm.names.length, length_getter, {}); m_data = (u8*)calloc(m_length, 1); } diff --git a/Libraries/LibLine/InternalFunctions.cpp b/Libraries/LibLine/InternalFunctions.cpp index 6cb80671f8..87a7d03b55 100644 --- a/Libraries/LibLine/InternalFunctions.cpp +++ b/Libraries/LibLine/InternalFunctions.cpp @@ -43,7 +43,7 @@ Function<bool(Editor&)> Editor::find_internal_function(const StringView& name) ENUMERATE_EDITOR_INTERNAL_FUNCTIONS(__ENUMERATE) - return nullptr; + return {}; } void Editor::search_forwards() diff --git a/Libraries/LibMarkdown/CodeBlock.cpp b/Libraries/LibMarkdown/CodeBlock.cpp index 2e7d533e7c..a869dea26e 100644 --- a/Libraries/LibMarkdown/CodeBlock.cpp +++ b/Libraries/LibMarkdown/CodeBlock.cpp @@ -112,13 +112,13 @@ String CodeBlock::render_for_terminal(size_t) const OwnPtr<CodeBlock> CodeBlock::parse(Vector<StringView>::ConstIterator& lines) { if (lines.is_end()) - return nullptr; + return {}; constexpr auto tick_tick_tick = "```"; StringView line = *lines; if (!line.starts_with(tick_tick_tick)) - return nullptr; + return {}; // Our Markdown extension: we allow // specifying a style and a language @@ -134,7 +134,7 @@ OwnPtr<CodeBlock> CodeBlock::parse(Vector<StringView>::ConstIterator& lines) StringView style_spec = line.substring_view(3, line.length() - 3); auto spec = Text::parse(style_spec); if (!spec.has_value()) - return nullptr; + return {}; ++lines; diff --git a/Libraries/LibMarkdown/Document.cpp b/Libraries/LibMarkdown/Document.cpp index bfd025a4dd..3b08dd2315 100644 --- a/Libraries/LibMarkdown/Document.cpp +++ b/Libraries/LibMarkdown/Document.cpp @@ -119,7 +119,7 @@ OwnPtr<Document> Document::parse(const StringView& str) auto line = Paragraph::Line::parse(lines); if (!line) - return nullptr; + return {}; paragraph_lines.append(line.release_nonnull()); } diff --git a/Libraries/LibMarkdown/Heading.cpp b/Libraries/LibMarkdown/Heading.cpp index a29686ef03..d3d05f7a38 100644 --- a/Libraries/LibMarkdown/Heading.cpp +++ b/Libraries/LibMarkdown/Heading.cpp @@ -62,7 +62,7 @@ String Heading::render_for_terminal(size_t) const OwnPtr<Heading> Heading::parse(Vector<StringView>::ConstIterator& lines) { if (lines.is_end()) - return nullptr; + return {}; const StringView& line = *lines; size_t level; @@ -73,12 +73,12 @@ OwnPtr<Heading> Heading::parse(Vector<StringView>::ConstIterator& lines) } if (!level || level >= line.length() || line[level] != ' ') - return nullptr; + return {}; StringView title_view = line.substring_view(level + 1, line.length() - level - 1); auto text = Text::parse(title_view); if (!text.has_value()) - return nullptr; + return {}; auto heading = make<Heading>(move(text.value()), level); diff --git a/Libraries/LibMarkdown/HorizontalRule.cpp b/Libraries/LibMarkdown/HorizontalRule.cpp index 200d5823bd..3aa59349b2 100644 --- a/Libraries/LibMarkdown/HorizontalRule.cpp +++ b/Libraries/LibMarkdown/HorizontalRule.cpp @@ -47,19 +47,19 @@ String HorizontalRule::render_for_terminal(size_t view_width) const OwnPtr<HorizontalRule> HorizontalRule::parse(Vector<StringView>::ConstIterator& lines) { if (lines.is_end()) - return nullptr; + return {}; const StringView& line = *lines; if (line.length() < 3) - return nullptr; + return {}; if (!line.starts_with('-') && !line.starts_with('_') && !line.starts_with('*')) - return nullptr; + return {}; auto first_character = line.characters_without_null_termination()[0]; for (auto ch : line) { if (ch != first_character) - return nullptr; + return {}; } ++lines; diff --git a/Libraries/LibMarkdown/List.cpp b/Libraries/LibMarkdown/List.cpp index 2f043ea59c..d9484cb098 100644 --- a/Libraries/LibMarkdown/List.cpp +++ b/Libraries/LibMarkdown/List.cpp @@ -122,20 +122,20 @@ OwnPtr<List> List::parse(Vector<StringView>::ConstIterator& lines) if (first) is_ordered = appears_ordered; else if (is_ordered != appears_ordered) - return nullptr; + return {}; if (!flush_item_if_needed()) - return nullptr; + return {}; while (offset + 1 < line.length() && line[offset + 1] == ' ') offset++; } else { if (first) - return nullptr; + return {}; for (size_t i = 0; i < offset; i++) { if (line[i] != ' ') - return nullptr; + return {}; } } @@ -149,7 +149,7 @@ OwnPtr<List> List::parse(Vector<StringView>::ConstIterator& lines) } if (!flush_item_if_needed() || first) - return nullptr; + return {}; return make<List>(move(items), is_ordered); } diff --git a/Libraries/LibMarkdown/Paragraph.cpp b/Libraries/LibMarkdown/Paragraph.cpp index 0e2b7f9df4..6a02289ae2 100644 --- a/Libraries/LibMarkdown/Paragraph.cpp +++ b/Libraries/LibMarkdown/Paragraph.cpp @@ -61,11 +61,11 @@ String Paragraph::render_for_terminal(size_t) const OwnPtr<Paragraph::Line> Paragraph::Line::parse(Vector<StringView>::ConstIterator& lines) { if (lines.is_end()) - return nullptr; + return {}; auto text = Text::parse(*lines++); if (!text.has_value()) - return nullptr; + return {}; return make<Paragraph::Line>(text.release_value()); } diff --git a/Libraries/LibMarkdown/Table.cpp b/Libraries/LibMarkdown/Table.cpp index 9298a37e94..b33fb9c532 100644 --- a/Libraries/LibMarkdown/Table.cpp +++ b/Libraries/LibMarkdown/Table.cpp @@ -118,12 +118,12 @@ OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines) auto peek_it = lines; auto first_line = *peek_it; if (!first_line.starts_with('|')) - return nullptr; + return {}; ++peek_it; if (peek_it.is_end()) - return nullptr; + return {}; auto header_segments = first_line.split_view('|', true); auto header_delimiters = peek_it->split_view('|', true); @@ -141,10 +141,10 @@ OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines) ++peek_it; if (header_delimiters.size() != header_segments.size()) - return nullptr; + return {}; if (header_delimiters.is_empty()) - return nullptr; + return {}; size_t total_width = 0; @@ -154,7 +154,7 @@ OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines) for (size_t i = 0; i < header_segments.size(); ++i) { auto text_option = Text::parse(header_segments[i]); if (!text_option.has_value()) - return nullptr; // An invalid 'text' in the header should just fail the table parse. + return {}; // An invalid 'text' in the header should just fail the table parse. auto text = text_option.release_value(); auto& column = table->m_columns[i]; diff --git a/Libraries/LibPCIDB/Database.cpp b/Libraries/LibPCIDB/Database.cpp index 8afc5a2f2d..597868ae1a 100644 --- a/Libraries/LibPCIDB/Database.cpp +++ b/Libraries/LibPCIDB/Database.cpp @@ -138,10 +138,10 @@ int Database::init() ParseMode mode = ParseMode::UnknownMode; - OwnPtr<Vendor> current_vendor = nullptr; - OwnPtr<Device> current_device = nullptr; - OwnPtr<Class> current_class = nullptr; - OwnPtr<Subclass> current_subclass = nullptr; + OwnPtr<Vendor> current_vendor {}; + OwnPtr<Device> current_device {}; + OwnPtr<Class> current_class {}; + OwnPtr<Subclass> current_subclass {}; auto commit_device = [&]() { if (current_device && current_vendor) { diff --git a/Libraries/LibRegex/C/Regex.cpp b/Libraries/LibRegex/C/Regex.cpp index d914d2760c..ad39b18af5 100644 --- a/Libraries/LibRegex/C/Regex.cpp +++ b/Libraries/LibRegex/C/Regex.cpp @@ -69,7 +69,7 @@ int regcomp(regex_t* reg, const char* pattern, int cflags) // Note that subsequent uses of regcomp() without regfree() _will_ leak memory // This could've been prevented if libc provided a reginit() or similar, but it does not. - reg->__data = new internal_regex_t { 0, 0, nullptr, 0, ReError::REG_NOERR, {}, 0 }; + reg->__data = new internal_regex_t { 0, 0, {}, 0, ReError::REG_NOERR, {}, 0 }; auto preg = impl_from(reg); diff --git a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index a043527f5b..d029e77eb8 100644 --- a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -142,11 +142,11 @@ void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&) OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter() { if (!m_element) - return nullptr; + return {}; if (!m_element->bitmap()) { if (!m_element->create_bitmap()) - return nullptr; + return {}; } return make<Gfx::Painter>(*m_element->bitmap()); @@ -208,7 +208,7 @@ RefPtr<ImageData> CanvasRenderingContext2D::create_image_data(int width, int hei { if (!wrapper()) { dbgln("Hmm! Attempted to create ImageData for wrapper-less CRC2D."); - return nullptr; + return {}; } return ImageData::create_with_size(wrapper()->global_object(), width, height); } |