47 lines · plain
1.. title:: clang-tidy - bugprone-nondeterministic-pointer-iteration-order2 3bugprone-nondeterministic-pointer-iteration-order4=================================================5 6Finds nondeterministic usages of pointers in unordered containers.7 8One canonical example is iteration across a container of pointers.9 10.. code-block:: c++11 12 {13 int a = 1, b = 2;14 std::unordered_set<int *> UnorderedPtrSet = {&a, &b};15 for (auto i : UnorderedPtrSet)16 f(i);17 }18 19Another such example is sorting a container of pointers.20 21.. code-block:: c++22 23 {24 int a = 1, b = 2;25 std::vector<int *> VectorOfPtr = {&a, &b};26 std::sort(VectorOfPtr.begin(), VectorOfPtr.end());27 }28 29Iteration of a containers of pointers may present the order of different30pointers differently across different runs of a program. In some cases this31may be acceptable behavior, in others this may be unexpected behavior. This32check is advisory for this reason.33 34This check only detects range-based for loops over unordered sets and maps. It35also detects calls sorting-like algorithms on containers holding pointers.36Other similar usages will not be found and are false negatives.37 38 39Limitations40-----------41 42* This check currently does not check if a nondeterministic iteration order is43 likely to be a mistake, and instead marks all such iterations as bugprone.44* std::reference_wrapper is not considered yet.45* Only for loops are considered, other iterators can be included in46 improvements.47