Skip to content

[clang-tidy]: Check request - bugprone-unordered-equal-compare #173108

@denzor200

Description

@denzor200

std::equal should not be used to compare the ranges formed by the iterators from std::unordered_set, std::unordered_multiset, std::unordered_map, or std::unordered_multimap because the order in which the elements are stored in those containers may be different even if the two containers store the same elements.

When comparing entire containers or string views(since C++17) for equality, operator== for the corresponding type are usually preferred.
See https://en.cppreference.com/w/cpp/algorithm/equal.html for more details.

Example:

void bad_example() {
    std::unordered_set<int> a, b;
    // WARNING: bugprone-unordered-equal-compare
    bool wrong = std::equal(a.begin(), a.end(), b.begin());
    
    // OK - good comparison
    bool correct = (a == b);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions