142 lines · cpp
1// RUN: %check_clang_tidy %s llvm-use-ranges %t2 3// Test that the header is included4// CHECK-FIXES: #include "llvm/ADT/STLExtras.h"5 6namespace std {7 8template <typename T> class vector {9public:10 using iterator = T *;11 using const_iterator = const T *;12 13 iterator begin();14 iterator end();15 const_iterator begin() const;16 const_iterator end() const;17 const_iterator cbegin() const;18 const_iterator cend() const;19};20 21template <typename T> T* begin(T (&arr)[5]);22template <typename T> T* end(T (&arr)[5]);23 24template <class InputIt, class T>25InputIt find(InputIt first, InputIt last, const T &value);26 27template <class RandomIt>28void sort(RandomIt first, RandomIt last);29 30template <class RandomIt>31void stable_sort(RandomIt first, RandomIt last);32 33template <class InputIt, class UnaryPredicate>34bool all_of(InputIt first, InputIt last, UnaryPredicate p);35 36template <class InputIt, class UnaryFunction>37UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f);38 39template <class ForwardIt, class T>40ForwardIt remove(ForwardIt first, ForwardIt last, const T& value);41 42template <class ForwardIt>43ForwardIt min_element(ForwardIt first, ForwardIt last);44 45template <class InputIt1, class InputIt2>46bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2);47 48template <class InputIt1, class InputIt2>49bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2);50 51template <class InputIt, class OutputIt>52OutputIt copy(InputIt first, InputIt last, OutputIt d_first);53 54template <class ForwardIt, class T>55void fill(ForwardIt first, ForwardIt last, const T& value);56 57template <class BidirIt>58void reverse(BidirIt first, BidirIt last);59 60template <class ForwardIt>61ForwardIt unique(ForwardIt first, ForwardIt last);62 63template <class ForwardIt>64bool is_sorted(ForwardIt first, ForwardIt last);65 66template <class InputIt1, class InputIt2>67bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2);68 69} // namespace std70 71bool is_even(int x);72void double_ref(int& x);73 74void test_positive() {75 std::vector<int> vec;76 int arr[5] = {1, 2, 3, 4, 5};77 78 auto it1 = std::find(vec.begin(), vec.end(), 3);79 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm80 // CHECK-FIXES: auto it1 = llvm::find(vec, 3);81 82 auto it2 = std::find(std::begin(arr), std::end(arr), 3);83 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm84 // CHECK-FIXES: auto it2 = llvm::find(arr, 3);85 86 std::stable_sort(vec.begin(), vec.end());87 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm88 // CHECK-FIXES: llvm::stable_sort(vec);89 90 bool all = std::all_of(vec.begin(), vec.end(), is_even);91 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm92 // CHECK-FIXES: bool all = llvm::all_of(vec, is_even);93 94 std::for_each(vec.begin(), vec.end(), double_ref);95 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm96 // CHECK-FIXES: llvm::for_each(vec, double_ref);97 98 auto min_it = std::min_element(vec.begin(), vec.end());99 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use an LLVM range-based algorithm100 // CHECK-FIXES: auto min_it = llvm::min_element(vec);101 102 std::vector<int> vec2;103 bool eq = std::equal(vec.begin(), vec.end(), vec2.begin(), vec2.end());104 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use an LLVM range-based algorithm105 // CHECK-FIXES: bool eq = llvm::equal(vec, vec2);106 107 std::copy(vec.begin(), vec.end(), vec2.begin());108 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm109 // CHECK-FIXES: llvm::copy(vec, vec2.begin());110 111 std::fill(vec.begin(), vec.end(), 0);112 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm113 // CHECK-FIXES: llvm::fill(vec, 0);114 115 auto last = std::unique(vec.begin(), vec.end());116 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use an LLVM range-based algorithm117 // CHECK-FIXES: auto last = llvm::unique(vec);118 119 bool sorted = std::is_sorted(vec.begin(), vec.end());120 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use an LLVM range-based algorithm121 // CHECK-FIXES: bool sorted = llvm::is_sorted(vec);122 123 std::includes(vec.begin(), vec.end(), std::begin(arr), std::end(arr));124 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm125 // CHECK-FIXES: llvm::includes(vec, arr);126}127 128void test_negative() {129 std::vector<int> v;130 131 // can not use `llvm::sort` because of potential different ordering from `std::sort`.132 std::sort(v.begin(), v.end());133 134 //non-begin/end iterators135 auto it1 = std::find(v.begin() + 1, v.end(), 2);136 auto it2 = std::find(v.begin(), v.end() - 1, 2);137 138 // Using different containers (3-arg equal)139 std::vector<int> v2;140 bool eq = std::equal(v.begin(), v.end(), v2.begin());141}142