Skip to content

Commit

Permalink
Everywhere: Remove redundant inequality comparison operators
Browse files Browse the repository at this point in the history
C++20 can automatically synthesize `operator!=` from `operator==`, so
there is no point in writing such functions by hand if all they do is
call through to `operator==`.

This fixes a compile error with compilers that implement P2468 (Clang
16 currently). This paper restores the C++17 behavior that if both
`T::operator==(U)` and `T::operator!=(U)` exist, `U == T` won't be
rewritten in reverse to call `T::operator==(U)`. Removing `!=` operators
makes the rewriting possible again.
See https://reviews.llvm.org/D134529#3853062
  • Loading branch information
BertalanD authored and ADKaster committed Nov 6, 2022
1 parent 4e406b0 commit 4296425
Show file tree
Hide file tree
Showing 40 changed files with 1 addition and 180 deletions.
2 changes: 0 additions & 2 deletions AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class ByteBuffer {
return !__builtin_memcmp(data(), other.data(), size());
}

bool operator!=(ByteBuffer const& other) const { return !(*this == other); }

[[nodiscard]] u8& operator[](size_t i)
{
VERIFY(i < m_size);
Expand Down
6 changes: 0 additions & 6 deletions AK/Complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ class [[gnu::packed]] Complex {
return (this->real() == a.real()) && (this->imag() == a.imag());
}

template<AK::Concepts::Arithmetic U>
constexpr bool operator!=(Complex<U> const& a) const
{
return !(*this == a);
}

constexpr Complex<T> operator+()
{
return *this;
Expand Down
4 changes: 0 additions & 4 deletions AK/DistinctNumeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class DistinctNumeric {
{
return this->m_value == other.m_value;
}
constexpr bool operator!=(Self const& other) const
{
return this->m_value != other.m_value;
}

// Only implemented when `Incr` is true:
constexpr Self& operator++()
Expand Down
4 changes: 0 additions & 4 deletions AK/FlyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,12 @@ class FlyString {
bool is_null() const { return !m_impl; }

bool operator==(FlyString const& other) const { return m_impl == other.m_impl; }
bool operator!=(FlyString const& other) const { return m_impl != other.m_impl; }

bool operator==(String const&) const;
bool operator!=(String const& string) const { return !(*this == string); }

bool operator==(StringView) const;
bool operator!=(StringView string) const { return !(*this == string); }

bool operator==(char const*) const;
bool operator!=(char const* string) const { return !(*this == string); }

StringImpl const* impl() const { return m_impl; }
char const* characters() const { return m_impl ? m_impl->characters() : nullptr; }
Expand Down
3 changes: 0 additions & 3 deletions AK/IntrusiveList.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class IntrusiveList {
T& operator*() { return *m_value; }
auto operator->() { return m_value; }
bool operator==(Iterator const& other) const { return other.m_value == m_value; }
bool operator!=(Iterator const& other) const { return !(*this == other); }
Iterator& operator++()
{
m_value = IntrusiveList<T, Container, member>::next(m_value);
Expand Down Expand Up @@ -108,7 +107,6 @@ class IntrusiveList {
T& operator*() { return *m_value; }
auto operator->() { return m_value; }
bool operator==(ReverseIterator const& other) const { return other.m_value == m_value; }
bool operator!=(ReverseIterator const& other) const { return !(*this == other); }
ReverseIterator& operator++()
{
m_value = IntrusiveList<T, Container, member>::prev(m_value);
Expand All @@ -134,7 +132,6 @@ class IntrusiveList {
T const& operator*() const { return *m_value; }
auto operator->() const { return m_value; }
bool operator==(ConstIterator const& other) const { return other.m_value == m_value; }
bool operator!=(ConstIterator const& other) const { return !(*this == other); }
ConstIterator& operator++()
{
m_value = IntrusiveList<T, Container, member>::next(m_value);
Expand Down
4 changes: 0 additions & 4 deletions AK/JsonPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ class JsonPathElement {
}
return false;
}
bool operator!=(JsonPathElement const& other) const
{
return !(*this == other);
}

private:
Kind m_kind;
Expand Down
2 changes: 0 additions & 2 deletions AK/NonnullRefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,8 @@ class [[nodiscard]] NonnullRefPtr {
}

bool operator==(NonnullRefPtr const& other) const { return m_ptr == other.m_ptr; }
bool operator!=(NonnullRefPtr const& other) const { return m_ptr != other.m_ptr; }

bool operator==(NonnullRefPtr& other) { return m_ptr == other.m_ptr; }
bool operator!=(NonnullRefPtr& other) { return m_ptr != other.m_ptr; }

// clang-format off
private:
Expand Down
9 changes: 0 additions & 9 deletions AK/RefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,29 +260,20 @@ class [[nodiscard]] RefPtr {
ALWAYS_INLINE operator bool() { return !is_null(); }

bool operator==(std::nullptr_t) const { return is_null(); }
bool operator!=(std::nullptr_t) const { return !is_null(); }

bool operator==(RefPtr const& other) const { return as_ptr() == other.as_ptr(); }
bool operator!=(RefPtr const& other) const { return as_ptr() != other.as_ptr(); }

bool operator==(RefPtr& other) { return as_ptr() == other.as_ptr(); }
bool operator!=(RefPtr& other) { return as_ptr() != other.as_ptr(); }

template<typename U>
bool operator==(NonnullRefPtr<U> const& other) const { return as_ptr() == other.m_ptr; }
template<typename U>
bool operator!=(NonnullRefPtr<U> const& other) const { return as_ptr() != other.m_ptr; }

template<typename U>
bool operator==(NonnullRefPtr<U>& other) { return as_ptr() == other.m_ptr; }
template<typename U>
bool operator!=(NonnullRefPtr<U>& other) { return as_ptr() != other.m_ptr; }

bool operator==(T const* other) const { return as_ptr() == other; }
bool operator!=(T const* other) const { return as_ptr() != other; }

bool operator==(T* other) { return as_ptr() == other; }
bool operator!=(T* other) { return as_ptr() != other; }

ALWAYS_INLINE bool is_null() const { return !m_ptr; }

Expand Down
4 changes: 0 additions & 4 deletions AK/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,10 @@ class String {
[[nodiscard]] bool ends_with(char) const;

bool operator==(String const&) const;
bool operator!=(String const& other) const { return !(*this == other); }

bool operator==(StringView) const;
bool operator!=(StringView other) const { return !(*this == other); }

bool operator==(FlyString const&) const;
bool operator!=(FlyString const& other) const { return !(*this == other); }

bool operator<(String const&) const;
bool operator<(char const*) const;
Expand All @@ -222,7 +219,6 @@ class String {
bool operator<=(char const* other) const { return !(*this > other); }

bool operator==(char const* cstring) const;
bool operator!=(char const* cstring) const { return !(*this == cstring); }

[[nodiscard]] String isolated_copy() const;

Expand Down
4 changes: 0 additions & 4 deletions AK/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ struct MaskSpan {
{
return start == other.start && length == other.length;
}
bool operator!=(MaskSpan const& other) const
{
return !(*this == other);
}
};

namespace StringUtils {
Expand Down
5 changes: 0 additions & 5 deletions AK/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,6 @@ class StringView {
return m_length == 1 && *m_characters == c;
}

constexpr bool operator!=(char const* cstring) const
{
return !(*this == cstring);
}

#ifndef KERNEL
bool operator==(String const&) const;
#endif
Expand Down
1 change: 0 additions & 1 deletion AK/Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ class Time {
[[nodiscard]] bool is_negative() const { return m_seconds < 0; }

bool operator==(Time const& other) const { return this->m_seconds == other.m_seconds && this->m_nanoseconds == other.m_nanoseconds; }
bool operator!=(Time const& other) const { return !(*this == other); }
Time operator+(Time const& other) const;
Time& operator+=(Time const& other);
Time operator-(Time const& other) const;
Expand Down
9 changes: 0 additions & 9 deletions AK/UUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ String UUID::to_string() const
}
#endif

bool UUID::operator==(const UUID& other) const
{
for (size_t index = 0; index < 16; index++) {
if (m_uuid_buffer[index] != other.m_uuid_buffer[index])
return false;
}
return true;
}

bool UUID::is_zero() const
{
return all_of(m_uuid_buffer, [](auto const octet) { return octet == 0; });
Expand Down
7 changes: 1 addition & 6 deletions AK/UUID.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ class UUID {
UUID(StringView, Endianness endianness = Endianness::Little);
~UUID() = default;

bool operator==(const UUID&) const;
bool operator!=(const UUID& other) const { return !(*this == other); }
bool operator<=(const UUID&) const = delete;
bool operator>=(const UUID&) const = delete;
bool operator<(const UUID&) const = delete;
bool operator>(const UUID&) const = delete;
bool operator==(const UUID&) const = default;

#ifdef KERNEL
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const;
Expand Down
5 changes: 0 additions & 5 deletions AK/Utf16View.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ class Utf16CodePointIterator {
return (m_ptr == other.m_ptr) && (m_remaining_code_units == other.m_remaining_code_units);
}

bool operator!=(Utf16CodePointIterator const& other) const
{
return !(*this == other);
}

Utf16CodePointIterator& operator++();
u32 operator*() const;

Expand Down
4 changes: 0 additions & 4 deletions AK/Utf32View.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class Utf32CodePointIterator {
{
return m_ptr == other.m_ptr && m_length == other.m_length;
}
bool operator!=(Utf32CodePointIterator const& other) const
{
return !(*this == other);
}
Utf32CodePointIterator& operator++()
{
VERIFY(m_length > 0);
Expand Down
5 changes: 0 additions & 5 deletions Userland/Applications/Spreadsheet/Position.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ struct Position {
return row == other.row && column == other.column;
}

bool operator!=(Position const& other) const
{
return !(other == *this);
}

String to_cell_identifier(Sheet const& sheet) const;
URL to_url(Sheet const& sheet) const;

Expand Down
1 change: 0 additions & 1 deletion Userland/Libraries/LibDebug/DebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class DebugInfo {
}

bool operator==(SourcePosition const& other) const { return file_path == other.file_path && line_number == other.line_number; }
bool operator!=(SourcePosition const& other) const { return !(*this == other); }

static SourcePosition from_line_info(Dwarf::LineProgram::LineInfo const&);
};
Expand Down
1 change: 0 additions & 1 deletion Userland/Libraries/LibDeviceTree/FlattenedDeviceTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct FlattenedDeviceTreeReserveEntry {
BigEndian<u64> size;

bool operator==(FlattenedDeviceTreeReserveEntry const& other) const { return other.address == address && other.size == size; }
bool operator!=(FlattenedDeviceTreeReserveEntry const& other) const { return !(operator==(other)); }
};
static_assert(sizeof(FlattenedDeviceTreeReserveEntry) == 16, "FDT Memory Reservation entry size must match specification");

Expand Down
5 changes: 0 additions & 5 deletions Userland/Libraries/LibGUI/ModelIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ class ModelIndex {
return m_model == other.m_model && m_row == other.m_row && m_column == other.m_column && m_internal_data == other.m_internal_data;
}

bool operator!=(ModelIndex const& other) const
{
return !(*this == other);
}

Model const* model() const { return m_model; }

Variant data(ModelRole = ModelRole::Display) const;
Expand Down
5 changes: 0 additions & 5 deletions Userland/Libraries/LibGfx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,6 @@ class Color {
return m_value == other.m_value;
}

constexpr bool operator!=(Color const& other) const
{
return m_value != other.m_value;
}

String to_string() const;
String to_string_without_alpha() const;
static Optional<Color> from_string(StringView);
Expand Down
6 changes: 0 additions & 6 deletions Userland/Libraries/LibGfx/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ class Point {
return x() == other.x() && y() == other.y();
}

template<class U>
[[nodiscard]] bool operator!=(Point<U> const& other) const
{
return !(*this == other);
}

[[nodiscard]] Point<T> operator+(Point<T> const& other) const { return { m_x + other.m_x, m_y + other.m_y }; }

Point<T>& operator+=(Point<T> const& other)
Expand Down
6 changes: 0 additions & 6 deletions Userland/Libraries/LibGfx/Rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,6 @@ class Rect {
return location() == other.location() && size() == other.size();
}

template<class U>
[[nodiscard]] bool operator!=(Rect<U> const& other) const
{
return !(*this == other);
}

[[nodiscard]] Rect<T> operator*(T factor) const { return { m_location * factor, m_size * factor }; }

Rect<T>& operator*=(T factor)
Expand Down
6 changes: 0 additions & 6 deletions Userland/Libraries/LibGfx/Size.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ class Size {
return width() == other.width() && height() == other.height();
}

template<class U>
[[nodiscard]] constexpr bool operator!=(Size<U> const& other) const
{
return !(*this == other);
}

constexpr Size<T>& operator-=(Size<T> const& other)
{
m_width -= other.m_width;
Expand Down
1 change: 0 additions & 1 deletion Userland/Libraries/LibJS/Runtime/PropertyAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class PropertyAttributes {
}

bool operator==(PropertyAttributes const& other) const { return m_bits == other.m_bits; }
bool operator!=(PropertyAttributes const& other) const { return m_bits != other.m_bits; }

[[nodiscard]] u8 bits() const { return m_bits; }

Expand Down
5 changes: 0 additions & 5 deletions Userland/Libraries/LibLine/KeyCallbackMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ struct Key {
{
return other.key == key && other.modifiers == modifiers;
}

bool operator!=(Key const& other) const
{
return !(*this == other);
}
};

struct KeyCallback {
Expand Down
Loading

0 comments on commit 4296425

Please sign in to comment.