1// RUN: %check_clang_tidy %s modernize-shrink-to-fit %t
2
3namespace std {
4template <typename T> struct vector { void swap(vector &other); };
5}
6
7void f() {
8 std::vector<int> v;
9
10 std::vector<int>(v).swap(other&: v);
11 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should be used to reduce the capacity of a shrinkable container [modernize-shrink-to-fit]
12 // CHECK-FIXES: {{^ }}v.shrink_to_fit();{{$}}
13
14 std::vector<int> &vref = v;
15 std::vector<int>(vref).swap(other&: vref);
16 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
17 // CHECK-FIXES: {{^ }}vref.shrink_to_fit();{{$}}
18
19 std::vector<int> *vptr = &v;
20 std::vector<int>(*vptr).swap(other&: *vptr);
21 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
22 // CHECK-FIXES: {{^ }}vptr->shrink_to_fit();{{$}}
23}
24
25struct X {
26 std::vector<int> v;
27 void f() {
28 std::vector<int>(v).swap(other&: v);
29 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: the shrink_to_fit method should
30 // CHECK-FIXES: {{^ }}v.shrink_to_fit();{{$}}
31
32 std::vector<int> *vptr = &v;
33 std::vector<int>(*vptr).swap(other&: *vptr);
34 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: the shrink_to_fit method should
35 // CHECK-FIXES: {{^ }}vptr->shrink_to_fit();{{$}}
36 }
37};
38
39template <typename T> void g() {
40 std::vector<int> v;
41 std::vector<int>(v).swap(other&: v);
42 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
43 // CHECK-FIXES: {{^ }}v.shrink_to_fit();{{$}}
44
45 std::vector<T> v2;
46 std::vector<T>(v2).swap(v2);
47 // CHECK-FIXES: {{^ }}std::vector<T>(v2).swap(v2);{{$}}
48}
49
50template <typename T> void g2() {
51 std::vector<int> v;
52 std::vector<int>(v).swap(other&: v);
53 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
54 // CHECK-FIXES: {{^ }}v.shrink_to_fit();{{$}}
55
56 T v3;
57 T(v3).swap(v3);
58 // CHECK-FIXES: {{^ }}T(v3).swap(v3);{{$}}
59}
60
61#define COPY_AND_SWAP_INT_VEC(x) std::vector<int>(x).swap(x)
62// CHECK-FIXES: #define COPY_AND_SWAP_INT_VEC(x) std::vector<int>(x).swap(x)
63
64void h() {
65 g<int>();
66 g<double>();
67 g<bool>();
68 g2<std::vector<int>>();
69 std::vector<int> v;
70 COPY_AND_SWAP_INT_VEC(v);
71 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
72 // CHECK-FIXES: {{^ }}COPY_AND_SWAP_INT_VEC(v);{{$}}
73}
74
75void PR38315() {
76 typedef std::vector<int> Vector;
77 Vector v;
78 Vector(v).swap(other&: v);
79 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
80 // CHECK-FIXES: {{^ }}v.shrink_to_fit();{{$}}
81
82 using Vector2 = std::vector<int>;
83 Vector2 v2;
84 Vector2(v2).swap(other&: v2);
85 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the shrink_to_fit method should
86 // CHECK-FIXES: {{^ }}v2.shrink_to_fit();{{$}}
87}
88

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/shrink-to-fit.cpp