1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 10:47:35 +00:00

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.
This commit is contained in:
Lenny Maiorani 2021-01-10 16:29:28 -07:00 committed by Andreas Kling
parent 9dc44bf8c4
commit e6f907a155
105 changed files with 300 additions and 244 deletions

View file

@ -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;

View file

@ -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());
}

View file

@ -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);

View file

@ -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;

View file

@ -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);
}

View file

@ -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());
}

View file

@ -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];