Skip to content

Commit 68473ad

Browse files
committed
Check that .unwrap_or(LITERAL) doesn't trigger unwrap_or_default
1 parent 8428b16 commit 68473ad

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

tests/ui/or_fun_call.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ fn or_fun_call() {
7777
with_default_type.unwrap_or_default();
7878
//~^ unwrap_or_default
7979

80+
let with_default_literal = Some(1);
81+
with_default_literal.unwrap_or(0);
82+
// Do not lint because `.unwrap_or_default()` wouldn't be simpler
83+
84+
let with_default_literal = Some(1.0);
85+
with_default_literal.unwrap_or(0.0);
86+
// Do not lint because `.unwrap_or_default()` wouldn't be simpler
87+
88+
let with_default_literal = Some("foo");
89+
with_default_literal.unwrap_or("");
90+
// Do not lint because `.unwrap_or_default()` wouldn't be simpler
91+
8092
let self_default = None::<FakeDefault>;
8193
self_default.unwrap_or_else(<FakeDefault>::default);
8294
//~^ or_fun_call

tests/ui/or_fun_call.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ fn or_fun_call() {
7777
with_default_type.unwrap_or(u64::default());
7878
//~^ unwrap_or_default
7979

80+
let with_default_literal = Some(1);
81+
with_default_literal.unwrap_or(0);
82+
// Do not lint because `.unwrap_or_default()` wouldn't be simpler
83+
84+
let with_default_literal = Some(1.0);
85+
with_default_literal.unwrap_or(0.0);
86+
// Do not lint because `.unwrap_or_default()` wouldn't be simpler
87+
88+
let with_default_literal = Some("foo");
89+
with_default_literal.unwrap_or("");
90+
// Do not lint because `.unwrap_or_default()` wouldn't be simpler
91+
8092
let self_default = None::<FakeDefault>;
8193
self_default.unwrap_or(<FakeDefault>::default());
8294
//~^ or_fun_call

tests/ui/or_fun_call.stderr

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,175 +47,175 @@ LL | with_default_type.unwrap_or(u64::default());
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
4848

4949
error: function call inside of `unwrap_or`
50-
--> tests/ui/or_fun_call.rs:81:18
50+
--> tests/ui/or_fun_call.rs:93:18
5151
|
5252
LL | self_default.unwrap_or(<FakeDefault>::default());
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(<FakeDefault>::default)`
5454

5555
error: use of `unwrap_or` to construct default value
56-
--> tests/ui/or_fun_call.rs:85:18
56+
--> tests/ui/or_fun_call.rs:97:18
5757
|
5858
LL | real_default.unwrap_or(<FakeDefault as Default>::default());
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
6060

6161
error: use of `unwrap_or` to construct default value
62-
--> tests/ui/or_fun_call.rs:89:14
62+
--> tests/ui/or_fun_call.rs:101:14
6363
|
6464
LL | with_vec.unwrap_or(vec![]);
6565
| ^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
6666

6767
error: function call inside of `unwrap_or`
68-
--> tests/ui/or_fun_call.rs:93:21
68+
--> tests/ui/or_fun_call.rs:105:21
6969
|
7070
LL | without_default.unwrap_or(Foo::new());
7171
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(Foo::new)`
7272

7373
error: use of `or_insert` to construct default value
74-
--> tests/ui/or_fun_call.rs:97:19
74+
--> tests/ui/or_fun_call.rs:109:19
7575
|
7676
LL | map.entry(42).or_insert(String::new());
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
7878

7979
error: use of `or_insert` to construct default value
80-
--> tests/ui/or_fun_call.rs:101:23
80+
--> tests/ui/or_fun_call.rs:113:23
8181
|
8282
LL | map_vec.entry(42).or_insert(vec![]);
8383
| ^^^^^^^^^^^^^^^^^ help: try: `or_default()`
8484

8585
error: use of `or_insert` to construct default value
86-
--> tests/ui/or_fun_call.rs:105:21
86+
--> tests/ui/or_fun_call.rs:117:21
8787
|
8888
LL | btree.entry(42).or_insert(String::new());
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
9090

9191
error: use of `or_insert` to construct default value
92-
--> tests/ui/or_fun_call.rs:109:25
92+
--> tests/ui/or_fun_call.rs:121:25
9393
|
9494
LL | btree_vec.entry(42).or_insert(vec![]);
9595
| ^^^^^^^^^^^^^^^^^ help: try: `or_default()`
9696

9797
error: use of `unwrap_or` to construct default value
98-
--> tests/ui/or_fun_call.rs:113:21
98+
--> tests/ui/or_fun_call.rs:125:21
9999
|
100100
LL | let _ = stringy.unwrap_or(String::new());
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
102102

103103
error: function call inside of `ok_or`
104-
--> tests/ui/or_fun_call.rs:118:17
104+
--> tests/ui/or_fun_call.rs:130:17
105105
|
106106
LL | let _ = opt.ok_or(format!("{} world.", hello));
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ok_or_else(|| format!("{} world.", hello))`
108108

109109
error: function call inside of `unwrap_or`
110-
--> tests/ui/or_fun_call.rs:123:21
110+
--> tests/ui/or_fun_call.rs:135:21
111111
|
112112
LL | let _ = Some(1).unwrap_or(map[&1]);
113113
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| map[&1])`
114114

115115
error: function call inside of `unwrap_or`
116-
--> tests/ui/or_fun_call.rs:126:21
116+
--> tests/ui/or_fun_call.rs:138:21
117117
|
118118
LL | let _ = Some(1).unwrap_or(map[&1]);
119119
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| map[&1])`
120120

121121
error: function call inside of `or`
122-
--> tests/ui/or_fun_call.rs:151:35
122+
--> tests/ui/or_fun_call.rs:163:35
123123
|
124124
LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
125125
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_else(|| Some("b".to_string()))`
126126

127127
error: function call inside of `unwrap_or`
128-
--> tests/ui/or_fun_call.rs:194:18
128+
--> tests/ui/or_fun_call.rs:206:18
129129
|
130130
LL | None.unwrap_or(ptr_to_ref(s));
131131
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| ptr_to_ref(s))`
132132

133133
error: function call inside of `unwrap_or`
134-
--> tests/ui/or_fun_call.rs:202:14
134+
--> tests/ui/or_fun_call.rs:214:14
135135
|
136136
LL | None.unwrap_or(unsafe { ptr_to_ref(s) });
137137
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| unsafe { ptr_to_ref(s) })`
138138

139139
error: function call inside of `unwrap_or`
140-
--> tests/ui/or_fun_call.rs:205:14
140+
--> tests/ui/or_fun_call.rs:217:14
141141
|
142142
LL | None.unwrap_or( unsafe { ptr_to_ref(s) } );
143143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| unsafe { ptr_to_ref(s) })`
144144

145145
error: function call inside of `map_or`
146-
--> tests/ui/or_fun_call.rs:281:25
146+
--> tests/ui/or_fun_call.rs:293:25
147147
|
148148
LL | let _ = Some(4).map_or(g(), |v| v);
149149
| ^^^^^^^^^^^^^^^^^^ help: try: `map_or_else(g, |v| v)`
150150

151151
error: function call inside of `map_or`
152-
--> tests/ui/or_fun_call.rs:283:25
152+
--> tests/ui/or_fun_call.rs:295:25
153153
|
154154
LL | let _ = Some(4).map_or(g(), f);
155155
| ^^^^^^^^^^^^^^ help: try: `map_or_else(g, f)`
156156

157157
error: function call inside of `map_or`
158-
--> tests/ui/or_fun_call.rs:286:25
158+
--> tests/ui/or_fun_call.rs:298:25
159159
|
160160
LL | let _ = Some(4).map_or("asd".to_string().len() as i32, f);
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `map_or_else(|| "asd".to_string().len() as i32, f)`
162162

163163
error: use of `unwrap_or_else` to construct default value
164-
--> tests/ui/or_fun_call.rs:317:18
164+
--> tests/ui/or_fun_call.rs:329:18
165165
|
166166
LL | with_new.unwrap_or_else(Vec::new);
167167
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
168168

169169
error: use of `unwrap_or_else` to construct default value
170-
--> tests/ui/or_fun_call.rs:321:28
170+
--> tests/ui/or_fun_call.rs:333:28
171171
|
172172
LL | with_default_trait.unwrap_or_else(Default::default);
173173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
174174

175175
error: use of `unwrap_or_else` to construct default value
176-
--> tests/ui/or_fun_call.rs:325:27
176+
--> tests/ui/or_fun_call.rs:337:27
177177
|
178178
LL | with_default_type.unwrap_or_else(u64::default);
179179
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
180180

181181
error: use of `unwrap_or_else` to construct default value
182-
--> tests/ui/or_fun_call.rs:329:22
182+
--> tests/ui/or_fun_call.rs:341:22
183183
|
184184
LL | real_default.unwrap_or_else(<FakeDefault as Default>::default);
185185
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
186186

187187
error: use of `or_insert_with` to construct default value
188-
--> tests/ui/or_fun_call.rs:333:23
188+
--> tests/ui/or_fun_call.rs:345:23
189189
|
190190
LL | map.entry(42).or_insert_with(String::new);
191191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
192192

193193
error: use of `or_insert_with` to construct default value
194-
--> tests/ui/or_fun_call.rs:337:25
194+
--> tests/ui/or_fun_call.rs:349:25
195195
|
196196
LL | btree.entry(42).or_insert_with(String::new);
197197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
198198

199199
error: use of `unwrap_or_else` to construct default value
200-
--> tests/ui/or_fun_call.rs:341:25
200+
--> tests/ui/or_fun_call.rs:353:25
201201
|
202202
LL | let _ = stringy.unwrap_or_else(String::new);
203203
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
204204

205205
error: function call inside of `unwrap_or`
206-
--> tests/ui/or_fun_call.rs:383:17
206+
--> tests/ui/or_fun_call.rs:395:17
207207
|
208208
LL | let _ = opt.unwrap_or({ f() }); // suggest `.unwrap_or_else(f)`
209209
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(f)`
210210

211211
error: function call inside of `unwrap_or`
212-
--> tests/ui/or_fun_call.rs:388:17
212+
--> tests/ui/or_fun_call.rs:400:17
213213
|
214214
LL | let _ = opt.unwrap_or(f() + 1); // suggest `.unwrap_or_else(|| f() + 1)`
215215
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| f() + 1)`
216216

217217
error: function call inside of `unwrap_or`
218-
--> tests/ui/or_fun_call.rs:393:17
218+
--> tests/ui/or_fun_call.rs:405:17
219219
|
220220
LL | let _ = opt.unwrap_or({
221221
| _________________^
@@ -235,55 +235,55 @@ LL ~ });
235235
|
236236

237237
error: function call inside of `map_or`
238-
--> tests/ui/or_fun_call.rs:399:17
238+
--> tests/ui/or_fun_call.rs:411:17
239239
|
240240
LL | let _ = opt.map_or(f() + 1, |v| v); // suggest `.map_or_else(|| f() + 1, |v| v)`
241241
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `map_or_else(|| f() + 1, |v| v)`
242242

243243
error: use of `unwrap_or` to construct default value
244-
--> tests/ui/or_fun_call.rs:404:17
244+
--> tests/ui/or_fun_call.rs:416:17
245245
|
246246
LL | let _ = opt.unwrap_or({ i32::default() });
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
248248

249249
error: function call inside of `unwrap_or`
250-
--> tests/ui/or_fun_call.rs:411:21
250+
--> tests/ui/or_fun_call.rs:423:21
251251
|
252252
LL | let _ = opt_foo.unwrap_or(Foo { val: String::default() });
253253
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| Foo { val: String::default() })`
254254

255255
error: function call inside of `map_or`
256-
--> tests/ui/or_fun_call.rs:426:19
256+
--> tests/ui/or_fun_call.rs:438:19
257257
|
258258
LL | let _ = x.map_or(g(), |v| v);
259259
| ^^^^^^^^^^^^^^^^^^ help: try: `map_or_else(|_| g(), |v| v)`
260260

261261
error: function call inside of `map_or`
262-
--> tests/ui/or_fun_call.rs:428:19
262+
--> tests/ui/or_fun_call.rs:440:19
263263
|
264264
LL | let _ = x.map_or(g(), f);
265265
| ^^^^^^^^^^^^^^ help: try: `map_or_else(|_| g(), f)`
266266

267267
error: function call inside of `map_or`
268-
--> tests/ui/or_fun_call.rs:431:19
268+
--> tests/ui/or_fun_call.rs:443:19
269269
|
270270
LL | let _ = x.map_or("asd".to_string().len() as i32, f);
271271
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `map_or_else(|_| "asd".to_string().len() as i32, f)`
272272

273273
error: function call inside of `get_or_insert`
274-
--> tests/ui/or_fun_call.rs:442:15
274+
--> tests/ui/or_fun_call.rs:454:15
275275
|
276276
LL | let _ = x.get_or_insert(g());
277277
| ^^^^^^^^^^^^^^^^^^ help: try: `get_or_insert_with(g)`
278278

279279
error: function call inside of `and`
280-
--> tests/ui/or_fun_call.rs:452:15
280+
--> tests/ui/or_fun_call.rs:464:15
281281
|
282282
LL | let _ = x.and(g());
283283
| ^^^^^^^^ help: try: `and_then(|_| g())`
284284

285285
error: function call inside of `and`
286-
--> tests/ui/or_fun_call.rs:462:15
286+
--> tests/ui/or_fun_call.rs:474:15
287287
|
288288
LL | let _ = x.and(g());
289289
| ^^^^^^^^ help: try: `and_then(|_| g())`

0 commit comments

Comments
 (0)