A comparison given to a sorting algorithm uses <= or >=, which is no strict ordering

tx0/cpp:comparator-not-strictrecommendedwarningreliability/equalitydepth fastbug

What it finds

std::sort(v.begin(), v.end(), [](int a, int b) { return a <= b; }): the algorithms of the standard library that order elements need a comparison that is a strict weak ordering, false for two equal elements. <= and >= are true for them, and with such a comparison std::sort may read past the end of the range or loop; the behaviour is undefined. < or > orders the same elements correctly.

The rule looks at lambdas passed to the sorting, heap, merge and minimum or maximum algorithms of std, whose body is one return of <= or >= between the same expression of the first parameter and of the second, as a.rank <= b.rank. The binary searches are left alone: they only need the range to be split by the comparison, and <= against the value searched for is a valid way to find the other end.

From the rule's source, comparator_not_strict.rs.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/cpp:comparator-not-strict" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/cpp:comparator-not-strict] reason="..."

tx0 explain tx0/cpp:comparator-not-strict shows the same in the terminal. Found a false positive? Report it.