Skip to content

Commit

Permalink
QMap/QHash: mark isEmpty() / empty() as [[nodiscard]]
Browse files Browse the repository at this point in the history
Cone of shame, they should've been marked as such since day 1.

Change-Id: I9aaf6567e183e4ece5443f4fbf12eb3a251501d5
Pick-to: 6.5
Reviewed-by: Thiago Macieira <[email protected]>
(cherry picked from commit 091bb2f)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
  • Loading branch information
dangelog authored and Qt Cherry-pick Bot committed Dec 5, 2024
1 parent 3210c4c commit db4e408
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/corelib/tools/qhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ class QHash
#endif // Q_QDOC

inline qsizetype size() const noexcept { return d ? qsizetype(d->size) : 0; }

[[nodiscard]]
inline bool isEmpty() const noexcept { return !d || d->size == 0; }

inline qsizetype capacity() const noexcept { return d ? qsizetype(d->numBuckets >> 1) : 0; }
Expand Down Expand Up @@ -1363,6 +1365,7 @@ class QHash
size_t bucket_count() const noexcept { return d ? d->numBuckets : 0; }
static size_t max_bucket_count() noexcept { return Data::maxNumBuckets(); }

[[nodiscard]]
inline bool empty() const noexcept { return isEmpty(); }

private:
Expand Down Expand Up @@ -1609,6 +1612,7 @@ class QMultiHash

inline qsizetype size() const noexcept { return m_size; }

[[nodiscard]]
inline bool isEmpty() const noexcept { return !m_size; }

inline qsizetype capacity() const noexcept { return d ? qsizetype(d->numBuckets >> 1) : 0; }
Expand Down Expand Up @@ -2118,6 +2122,7 @@ class QMultiHash
size_t bucket_count() const noexcept { return d ? d->numBuckets : 0; }
static size_t max_bucket_count() noexcept { return Data::maxNumBuckets(); }

[[nodiscard]]
inline bool empty() const noexcept { return isEmpty(); }

inline iterator replace(const Key &key, const T &value)
Expand Down
4 changes: 4 additions & 0 deletions src/corelib/tools/qmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class QMap

size_type size() const { return d ? size_type(d->m.size()) : size_type(0); }

[[nodiscard]]
bool isEmpty() const { return d ? d->m.empty() : true; }

void detach()
Expand Down Expand Up @@ -759,6 +760,7 @@ class QMap
}

// STL compatibility
[[nodiscard]]
inline bool empty() const
{
return isEmpty();
Expand Down Expand Up @@ -938,6 +940,7 @@ class QMultiMap

size_type size() const { return d ? size_type(d->m.size()) : size_type(0); }

[[nodiscard]]
bool isEmpty() const { return d ? d->m.empty() : true; }

void detach()
Expand Down Expand Up @@ -1518,6 +1521,7 @@ class QMultiMap
}

// STL compatibility
[[nodiscard]]
inline bool empty() const { return isEmpty(); }

std::pair<iterator, iterator> equal_range(const Key &akey)
Expand Down

0 comments on commit db4e408

Please sign in to comment.