Skip to content

Commit 37222e5

Browse files
committed
Only suggest map_or for copy types
1 parent 11ba624 commit 37222e5

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,8 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr, ok_args: &[hir::Ex
17511751
/// lint use of `map().unwrap_or()` for `Option`s
17521752
fn lint_map_unwrap_or(cx: &LateContext<'_, '_>, expr: &hir::Expr, map_args: &[hir::Expr], unwrap_args: &[hir::Expr]) {
17531753
// lint if the caller of `map()` is an `Option`
1754-
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) {
1754+
let unwrap_ty = cx.tables.expr_ty(&unwrap_args[1]);
1755+
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) && is_copy(cx, unwrap_ty) {
17551756
// get snippets for args to map() and unwrap_or()
17561757
let map_snippet = snippet(cx, map_args[1].span, "..");
17571758
let unwrap_snippet = snippet(cx, unwrap_args[1].span, "..");

tests/ui/methods.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ fn option_methods() {
133133
// macro case
134134
let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
135135

136+
let id: String = "identifier".to_string();
137+
let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id); // Should not lint if not copyable
138+
136139
// Check OPTION_MAP_UNWRAP_OR_ELSE
137140
// single line case
138141
let _ = opt.map(|x| x + 1)

tests/ui/methods.stderr

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ LL | | .unwrap_or(None);
8888
= note: replace `map(|x| Some(x + 1)).unwrap_or(None)` with `and_then(|x| Some(x + 1))`
8989

9090
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
91-
--> $DIR/methods.rs:138:13
91+
--> $DIR/methods.rs:141:13
9292
|
9393
LL | let _ = opt.map(|x| x + 1)
9494
| _____________^
@@ -100,7 +100,7 @@ LL | | .unwrap_or_else(|| 0); // should lint even though this cal
100100
= note: replace `map(|x| x + 1).unwrap_or_else(|| 0)` with `map_or_else(|| 0, |x| x + 1)`
101101

102102
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
103-
--> $DIR/methods.rs:142:13
103+
--> $DIR/methods.rs:145:13
104104
|
105105
LL | let _ = opt.map(|x| {
106106
| _____________^
@@ -110,7 +110,7 @@ LL | | ).unwrap_or_else(|| 0);
110110
| |____________________________________^
111111

112112
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
113-
--> $DIR/methods.rs:146:13
113+
--> $DIR/methods.rs:149:13
114114
|
115115
LL | let _ = opt.map(|x| x + 1)
116116
| _____________^
@@ -120,15 +120,15 @@ LL | | );
120120
| |_________________^
121121

122122
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
123-
--> $DIR/methods.rs:155:13
123+
--> $DIR/methods.rs:158:13
124124
|
125125
LL | let _ = opt.map_or(None, |x| Some(x + 1));
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using and_then instead: `opt.and_then(|x| Some(x + 1))`
127127
|
128128
= note: `-D clippy::option-map-or-none` implied by `-D warnings`
129129

130130
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
131-
--> $DIR/methods.rs:157:13
131+
--> $DIR/methods.rs:160:13
132132
|
133133
LL | let _ = opt.map_or(None, |x| {
134134
| _____________^
@@ -144,7 +144,7 @@ LL | });
144144
|
145145

146146
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
147-
--> $DIR/methods.rs:182:13
147+
--> $DIR/methods.rs:185:13
148148
|
149149
LL | let _ = v.iter().filter(|&x| *x < 0).next();
150150
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -153,7 +153,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
153153
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
154154

155155
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
156-
--> $DIR/methods.rs:185:13
156+
--> $DIR/methods.rs:188:13
157157
|
158158
LL | let _ = v.iter().filter(|&x| {
159159
| _____________^
@@ -163,7 +163,7 @@ LL | | ).next();
163163
| |___________________________^
164164

165165
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
166-
--> $DIR/methods.rs:200:13
166+
--> $DIR/methods.rs:203:13
167167
|
168168
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
169169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -172,7 +172,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
172172
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
173173

174174
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
175-
--> $DIR/methods.rs:203:13
175+
--> $DIR/methods.rs:206:13
176176
|
177177
LL | let _ = v.iter().find(|&x| {
178178
| _____________^
@@ -182,15 +182,15 @@ LL | | ).is_some();
182182
| |______________________________^
183183

184184
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
185-
--> $DIR/methods.rs:209:13
185+
--> $DIR/methods.rs:212:13
186186
|
187187
LL | let _ = v.iter().position(|&x| x < 0).is_some();
188188
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189189
|
190190
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
191191

192192
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
193-
--> $DIR/methods.rs:212:13
193+
--> $DIR/methods.rs:215:13
194194
|
195195
LL | let _ = v.iter().position(|&x| {
196196
| _____________^
@@ -200,15 +200,15 @@ LL | | ).is_some();
200200
| |______________________________^
201201

202202
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
203-
--> $DIR/methods.rs:218:13
203+
--> $DIR/methods.rs:221:13
204204
|
205205
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
206206
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
207207
|
208208
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
209209

210210
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
211-
--> $DIR/methods.rs:221:13
211+
--> $DIR/methods.rs:224:13
212212
|
213213
LL | let _ = v.iter().rposition(|&x| {
214214
| _____________^
@@ -218,125 +218,125 @@ LL | | ).is_some();
218218
| |______________________________^
219219

220220
error: use of `unwrap_or` followed by a function call
221-
--> $DIR/methods.rs:256:22
221+
--> $DIR/methods.rs:259:22
222222
|
223223
LL | with_constructor.unwrap_or(make());
224224
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
225225
|
226226
= note: `-D clippy::or-fun-call` implied by `-D warnings`
227227

228228
error: use of `unwrap_or` followed by a call to `new`
229-
--> $DIR/methods.rs:259:5
229+
--> $DIR/methods.rs:262:5
230230
|
231231
LL | with_new.unwrap_or(Vec::new());
232232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
233233

234234
error: use of `unwrap_or` followed by a function call
235-
--> $DIR/methods.rs:262:21
235+
--> $DIR/methods.rs:265:21
236236
|
237237
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
238238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
239239

240240
error: use of `unwrap_or` followed by a function call
241-
--> $DIR/methods.rs:265:14
241+
--> $DIR/methods.rs:268:14
242242
|
243243
LL | with_err.unwrap_or(make());
244244
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
245245

246246
error: use of `unwrap_or` followed by a function call
247-
--> $DIR/methods.rs:268:19
247+
--> $DIR/methods.rs:271:19
248248
|
249249
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
250250
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
251251

252252
error: use of `unwrap_or` followed by a call to `default`
253-
--> $DIR/methods.rs:271:5
253+
--> $DIR/methods.rs:274:5
254254
|
255255
LL | with_default_trait.unwrap_or(Default::default());
256256
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
257257

258258
error: use of `unwrap_or` followed by a call to `default`
259-
--> $DIR/methods.rs:274:5
259+
--> $DIR/methods.rs:277:5
260260
|
261261
LL | with_default_type.unwrap_or(u64::default());
262262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
263263

264264
error: use of `unwrap_or` followed by a function call
265-
--> $DIR/methods.rs:277:14
265+
--> $DIR/methods.rs:280:14
266266
|
267267
LL | with_vec.unwrap_or(vec![]);
268268
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
269269

270270
error: use of `unwrap_or` followed by a function call
271-
--> $DIR/methods.rs:282:21
271+
--> $DIR/methods.rs:285:21
272272
|
273273
LL | without_default.unwrap_or(Foo::new());
274274
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
275275

276276
error: use of `or_insert` followed by a function call
277-
--> $DIR/methods.rs:285:19
277+
--> $DIR/methods.rs:288:19
278278
|
279279
LL | map.entry(42).or_insert(String::new());
280280
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
281281

282282
error: use of `or_insert` followed by a function call
283-
--> $DIR/methods.rs:288:21
283+
--> $DIR/methods.rs:291:21
284284
|
285285
LL | btree.entry(42).or_insert(String::new());
286286
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
287287

288288
error: use of `unwrap_or` followed by a function call
289-
--> $DIR/methods.rs:291:21
289+
--> $DIR/methods.rs:294:21
290290
|
291291
LL | let _ = stringy.unwrap_or("".to_owned());
292292
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
293293

294294
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
295-
--> $DIR/methods.rs:302:23
295+
--> $DIR/methods.rs:305:23
296296
|
297297
LL | let bad_vec = some_vec.iter().nth(3);
298298
| ^^^^^^^^^^^^^^^^^^^^^^
299299
|
300300
= note: `-D clippy::iter-nth` implied by `-D warnings`
301301

302302
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
303-
--> $DIR/methods.rs:303:26
303+
--> $DIR/methods.rs:306:26
304304
|
305305
LL | let bad_slice = &some_vec[..].iter().nth(3);
306306
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
307307

308308
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
309-
--> $DIR/methods.rs:304:31
309+
--> $DIR/methods.rs:307:31
310310
|
311311
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
312312
| ^^^^^^^^^^^^^^^^^^^^^^^^^
313313

314314
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
315-
--> $DIR/methods.rs:305:29
315+
--> $DIR/methods.rs:308:29
316316
|
317317
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
318318
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
319319

320320
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
321-
--> $DIR/methods.rs:310:23
321+
--> $DIR/methods.rs:313:23
322322
|
323323
LL | let bad_vec = some_vec.iter_mut().nth(3);
324324
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
325325

326326
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
327-
--> $DIR/methods.rs:313:26
327+
--> $DIR/methods.rs:316:26
328328
|
329329
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
330330
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
331331

332332
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
333-
--> $DIR/methods.rs:316:29
333+
--> $DIR/methods.rs:319:29
334334
|
335335
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
336336
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337337

338338
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
339-
--> $DIR/methods.rs:328:13
339+
--> $DIR/methods.rs:331:13
340340
|
341341
LL | let _ = opt.unwrap();
342342
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)