1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
// RUN: %check_clang_tidy -std=c++11,c++14 %s bugprone-inaccurate-erase %t
// FIXME: Fix the checker to work in C++17 mode.
namespace std {
template <typename T> struct vec_iterator {
T ptr;
vec_iterator operator++(int);
template <typename X>
vec_iterator(const vec_iterator<X> &); // Omit enable_if<...>.
};
template <typename T> struct vector {
typedef vec_iterator<T*> iterator;
iterator begin();
iterator end();
void erase(iterator);
void erase(iterator, iterator);
};
template <typename T> struct vector_with_const_iterator {
typedef vec_iterator<T*> iterator;
typedef vec_iterator<const T*> const_iterator;
iterator begin();
iterator end();
void erase(const_iterator);
void erase(const_iterator, const_iterator);
};
template <typename FwIt, typename T>
FwIt remove(FwIt begin, FwIt end, const T &val);
template <typename FwIt, typename Func>
FwIt remove_if(FwIt begin, FwIt end, Func f);
template <typename FwIt> FwIt unique(FwIt begin, FwIt end);
template <typename T> struct unique_ptr {};
} // namespace std
struct custom_iter {};
struct custom_container {
void erase(...);
custom_iter begin();
custom_iter end();
};
template <typename T> void g() {
T t;
t.erase(std::remove(t.begin(), t.end(), 10));
// CHECK-FIXES: {{^ }}t.erase(std::remove(t.begin(), t.end(), 10));{{$}}
std::vector<int> v;
v.erase(remove(v.begin(), v.end(), 10));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this call will remove at most one
// CHECK-FIXES: {{^ }}v.erase(remove(v.begin(), v.end(), 10), v.end());{{$}}
}
#define ERASE(x, y) x.erase(remove(x.begin(), x.end(), y))
// CHECK-FIXES: #define ERASE(x, y) x.erase(remove(x.begin(), x.end(), y))
int main() {
std::vector<int> v;
v.erase(remove(v.begin(), v.end(), 10));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this call will remove at most one item even when multiple items should be removed [bugprone-inaccurate-erase]
// CHECK-FIXES: {{^ }}v.erase(remove(v.begin(), v.end(), 10), v.end());{{$}}
v.erase(remove(v.begin(), v.end(), 20), v.end());
auto *p = &v;
p->erase(remove(p->begin(), p->end(), 11));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this call will remove at most one
// CHECK-FIXES: {{^ }}p->erase(remove(p->begin(), p->end(), 11), p->end());{{$}}
std::vector_with_const_iterator<int> v2;
v2.erase(remove(v2.begin(), v2.end(), 12));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this call will remove at most one
// CHECK-FIXES: {{^ }}v2.erase(remove(v2.begin(), v2.end(), 12), v2.end());{{$}}
// Fix is not trivial.
auto it = v.end();
v.erase(remove(v.begin(), it, 10));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this call will remove at most one
// CHECK-FIXES: {{^ }}v.erase(remove(v.begin(), it, 10));{{$}}
g<std::vector<int>>();
g<custom_container>();
ERASE(v, 15);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: this call will remove at most one
// CHECK-FIXES: {{^ }}ERASE(v, 15);{{$}}
std::vector<std::unique_ptr<int>> vupi;
auto iter = vupi.begin();
vupi.erase(iter++);
// CHECK-FIXES: {{^ }}vupi.erase(iter++);{{$}}
}
|