OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 | 8 |
9 #define true true | 9 #define true true |
10 | 10 |
11 base::ListValue* ReturnsRawPtr() { | |
12 return nullptr; | |
13 } | |
14 | |
11 std::unique_ptr<base::Value> ReturnsUniquePtr() { | 15 std::unique_ptr<base::Value> ReturnsUniquePtr() { |
12 return nullptr; | 16 return nullptr; |
13 } | 17 } |
14 | 18 |
19 // The joy of raw pointers. | |
20 void DoesItTakeOwnership(base::Value*) {} | |
21 | |
15 struct Thing { | 22 struct Thing { |
16 std::unique_ptr<base::Value> ToValue() { return nullptr; } | 23 std::unique_ptr<base::Value> ToValue() { return nullptr; } |
17 }; | 24 }; |
18 | 25 |
19 void F() { | 26 void F() { |
20 base::ListValue list; | 27 base::ListValue list; |
21 list.Append(new base::FundamentalValue(1 == 0)); | 28 list.Append(new base::FundamentalValue(1 == 0)); |
22 list.Append(new base::FundamentalValue(true)); | 29 list.Append(new base::FundamentalValue(true)); |
23 list.Append(new base::FundamentalValue(static_cast<unsigned char>(1.0))); | 30 list.Append(new base::FundamentalValue(static_cast<unsigned char>(1.0))); |
24 list.Append(new base::FundamentalValue(double{3})); | 31 list.Append(new base::FundamentalValue(double{3})); |
25 list.Append(new base::StringValue("abc")); | 32 list.Append(new base::StringValue("abc")); |
26 | 33 |
27 list.Append(ReturnsUniquePtr().release()); | 34 list.Append(ReturnsUniquePtr().release()); |
28 Thing thing; | 35 Thing thing; |
29 list.Append(thing.ToValue().release()); | 36 list.Append(thing.ToValue().release()); |
30 std::unique_ptr<base::Value> unique_ptr_var; | 37 std::unique_ptr<base::Value> unique_ptr_var; |
31 list.Append(unique_ptr_var.release()); | 38 list.Append(unique_ptr_var.release()); |
32 } | 39 } |
40 | |
41 void G(base::Value* input) { | |
42 base::ListValue list; | |
43 | |
44 base::ListValue* local = new base::ListValue(); | |
45 // Not rewritten, since it often makes more sense to change the function | |
46 // prototype. | |
47 local->Append(input); | |
48 // Should be rewritten: it will only be moved after it's no longer referenced. | |
49 list.Append(local); | |
50 | |
51 // Not rewritten, since it would be used after it's moved. In theory, we could | |
52 // automatically handle this too, but the risk of accidentally breaking | |
53 // something is much higher. | |
54 base::ListValue* clever_list = new base::ListValue; | |
55 list.Append(clever_list); | |
56 clever_list->AppendInteger(2); | |
57 | |
58 // Not rewritten, since it often makes more sense to change the function | |
59 // prototype. | |
60 base::Value* returned_value = ReturnsRawPtr(); | |
61 list.Append(returned_value); | |
62 | |
63 // Should be rewritten. The reassignment should be transformed into | |
64 // .reset(). | |
65 base::ListValue* reused_list = new base::ListValue; | |
66 reused_list->AppendInteger(1); | |
67 list.Append(reused_list); | |
68 reused_list = new base::ListValue; | |
69 reused_list->AppendInteger(3); | |
70 list.Append(reused_list); | |
71 | |
72 // This shouldn't be rewritten, since the reassignment is the return | |
73 // value of a function. | |
74 base::ListValue* reused_list_2 = new base::ListValue; | |
75 reused_list_2->AppendInteger(4); | |
76 list.Append(reused_list_2); | |
77 reused_list_2 = ReturnsRawPtr(); | |
78 reused_list_2->AppendInteger(5); | |
79 list.Append(reused_list_2); | |
80 | |
81 // auto should be expanded to a std::unique_ptr containing the deduced type. | |
82 auto* auto_list = new base::ListValue; | |
83 auto_list->AppendInteger(6); | |
84 list.Append(auto_list); | |
85 | |
86 auto auto_list_2 = new base::ListValue; | |
87 auto_list_2->AppendInteger(7); | |
88 list.Append(auto_list_2); | |
89 | |
90 // Shouldn't be rewritten: a raw pointer is passed to a function which may or | |
91 // may not take ownership. | |
92 base::ListValue* maybe_owned_list = new base::ListValue; | |
93 DoesItTakeOwnership(maybe_owned_list); | |
94 list.Append(maybe_owned_list); | |
95 | |
96 // Should be rewritten, even though it doesn't have an initializer. | |
97 base::ListValue* list_with_no_initializer; | |
98 list_with_no_initializer = new base::ListValue; | |
99 list.Append(list_with_no_initializer); | |
100 | |
101 // Make sure C++98 style initialization is correctly handled. | |
102 base::ListValue* cxx98_list(new base::ListValue); | |
103 list.Append(cxx98_list); | |
104 | |
105 // And C++11 style initialization as well. | |
106 base::ListValue* cxx11_list(new base::ListValue); | |
danakj
2016/06/10 21:59:55
This is the same as cxx98?
dcheng
2016/06/10 22:39:43
Done.
| |
107 list.Append(cxx11_list); | |
108 } | |
OLD | NEW |