Skip to content

Commit 54ab22f

Browse files
committed
Only suggest map_or for copy types
1 parent 412d41a commit 54ab22f

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
@@ -1772,7 +1772,8 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr, ok_args: &[hir::Ex
17721772
/// lint use of `map().unwrap_or()` for `Option`s
17731773
fn lint_map_unwrap_or(cx: &LateContext<'_, '_>, expr: &hir::Expr, map_args: &[hir::Expr], unwrap_args: &[hir::Expr]) {
17741774
// lint if the caller of `map()` is an `Option`
1775-
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) {
1775+
let unwrap_ty = cx.tables.expr_ty(&unwrap_args[1]);
1776+
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) && is_copy(cx, unwrap_ty) {
17761777
// get snippets for args to map() and unwrap_or()
17771778
let map_snippet = snippet(cx, map_args[1].span, "..");
17781779
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
@@ -179,6 +179,9 @@ fn option_methods() {
179179
// macro case
180180
let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
181181

182+
let id: String = "identifier".to_string();
183+
let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id); // Should not lint if not copyable
184+
182185
// Check OPTION_MAP_UNWRAP_OR_ELSE
183186
// single line case
184187
let _ = opt.map(|x| x + 1)

tests/ui/methods.stderr

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

9292
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
93-
--> $DIR/methods.rs:184:13
93+
--> $DIR/methods.rs:187:13
9494
|
9595
LL | let _ = opt.map(|x| x + 1)
9696
| _____________^
@@ -102,7 +102,7 @@ LL | | .unwrap_or_else(|| 0); // should lint even though this cal
102102
= note: replace `map(|x| x + 1).unwrap_or_else(|| 0)` with `map_or_else(|| 0, |x| x + 1)`
103103

104104
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
105-
--> $DIR/methods.rs:188:13
105+
--> $DIR/methods.rs:191:13
106106
|
107107
LL | let _ = opt.map(|x| {
108108
| _____________^
@@ -112,7 +112,7 @@ LL | | ).unwrap_or_else(|| 0);
112112
| |____________________________________^
113113

114114
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
115-
--> $DIR/methods.rs:192:13
115+
--> $DIR/methods.rs:195:13
116116
|
117117
LL | let _ = opt.map(|x| x + 1)
118118
| _____________^
@@ -122,15 +122,15 @@ LL | | );
122122
| |_________________^
123123

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

132132
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
133-
--> $DIR/methods.rs:203:13
133+
--> $DIR/methods.rs:206:13
134134
|
135135
LL | let _ = opt.map_or(None, |x| {
136136
| _____________^
@@ -146,7 +146,7 @@ LL | });
146146
|
147147

148148
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
149-
--> $DIR/methods.rs:229:13
149+
--> $DIR/methods.rs:232:13
150150
|
151151
LL | let _ = v.iter().filter(|&x| *x < 0).next();
152152
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -155,7 +155,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
155155
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
156156

157157
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
158-
--> $DIR/methods.rs:232:13
158+
--> $DIR/methods.rs:235:13
159159
|
160160
LL | let _ = v.iter().filter(|&x| {
161161
| _____________^
@@ -165,7 +165,7 @@ LL | | ).next();
165165
| |___________________________^
166166

167167
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
168-
--> $DIR/methods.rs:248:13
168+
--> $DIR/methods.rs:251:13
169169
|
170170
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
171171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -174,7 +174,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
174174
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
175175

176176
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
177-
--> $DIR/methods.rs:251:13
177+
--> $DIR/methods.rs:254:13
178178
|
179179
LL | let _ = v.iter().find(|&x| {
180180
| _____________^
@@ -184,15 +184,15 @@ LL | | ).is_some();
184184
| |______________________________^
185185

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

194194
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
195-
--> $DIR/methods.rs:260:13
195+
--> $DIR/methods.rs:263:13
196196
|
197197
LL | let _ = v.iter().position(|&x| {
198198
| _____________^
@@ -202,15 +202,15 @@ LL | | ).is_some();
202202
| |______________________________^
203203

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

212212
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
213-
--> $DIR/methods.rs:269:13
213+
--> $DIR/methods.rs:272:13
214214
|
215215
LL | let _ = v.iter().rposition(|&x| {
216216
| _____________^
@@ -220,125 +220,125 @@ LL | | ).is_some();
220220
| |______________________________^
221221

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

230230
error: use of `unwrap_or` followed by a call to `new`
231-
--> $DIR/methods.rs:309:5
231+
--> $DIR/methods.rs:312:5
232232
|
233233
LL | with_new.unwrap_or(Vec::new());
234234
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
235235

236236
error: use of `unwrap_or` followed by a function call
237-
--> $DIR/methods.rs:312:21
237+
--> $DIR/methods.rs:315:21
238238
|
239239
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
240240
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
241241

242242
error: use of `unwrap_or` followed by a function call
243-
--> $DIR/methods.rs:315:14
243+
--> $DIR/methods.rs:318:14
244244
|
245245
LL | with_err.unwrap_or(make());
246246
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
247247

248248
error: use of `unwrap_or` followed by a function call
249-
--> $DIR/methods.rs:318:19
249+
--> $DIR/methods.rs:321:19
250250
|
251251
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
252252
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
253253

254254
error: use of `unwrap_or` followed by a call to `default`
255-
--> $DIR/methods.rs:321:5
255+
--> $DIR/methods.rs:324:5
256256
|
257257
LL | with_default_trait.unwrap_or(Default::default());
258258
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
259259

260260
error: use of `unwrap_or` followed by a call to `default`
261-
--> $DIR/methods.rs:324:5
261+
--> $DIR/methods.rs:327:5
262262
|
263263
LL | with_default_type.unwrap_or(u64::default());
264264
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
265265

266266
error: use of `unwrap_or` followed by a function call
267-
--> $DIR/methods.rs:327:14
267+
--> $DIR/methods.rs:330:14
268268
|
269269
LL | with_vec.unwrap_or(vec![]);
270270
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
271271

272272
error: use of `unwrap_or` followed by a function call
273-
--> $DIR/methods.rs:332:21
273+
--> $DIR/methods.rs:335:21
274274
|
275275
LL | without_default.unwrap_or(Foo::new());
276276
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
277277

278278
error: use of `or_insert` followed by a function call
279-
--> $DIR/methods.rs:335:19
279+
--> $DIR/methods.rs:338:19
280280
|
281281
LL | map.entry(42).or_insert(String::new());
282282
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
283283

284284
error: use of `or_insert` followed by a function call
285-
--> $DIR/methods.rs:338:21
285+
--> $DIR/methods.rs:341:21
286286
|
287287
LL | btree.entry(42).or_insert(String::new());
288288
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
289289

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

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

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

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

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

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

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

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

340340
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
341-
--> $DIR/methods.rs:378:13
341+
--> $DIR/methods.rs:381:13
342342
|
343343
LL | let _ = opt.unwrap();
344344
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)