Skip to content

Commit 215cfed

Browse files
committed
add tests for partial moves
1 parent 0384c20 commit 215cfed

File tree

2 files changed

+86
-15
lines changed

2 files changed

+86
-15
lines changed

tests/ui/checked_unwrap/simple_conditionals.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,24 @@ fn check_expect() {
289289
}
290290
}
291291

292+
fn partial_moves() {
293+
fn borrow_option(_: &Option<()>) {}
294+
295+
let x = Some(());
296+
// Using `if let Some(o) = x` won't work here, as `borrow_option` will try to borrow a moved value
297+
if x.is_some() {
298+
borrow_option(&x);
299+
x.unwrap();
300+
//~^ unnecessary_unwrap
301+
}
302+
// This is fine though, as `if let Some(o) = &x` won't move `x`
303+
if x.is_some() {
304+
borrow_option(&x);
305+
x.as_ref().unwrap();
306+
//~^ unnecessary_unwrap
307+
}
308+
}
309+
292310
fn issue15321() {
293311
struct Soption {
294312
option: Option<bool>,
@@ -436,4 +454,21 @@ fn issue15321() {
436454
sopt2.option.option = None;
437455
sopt2.option.option.unwrap()
438456
};
457+
458+
// Partial moves
459+
fn borrow_toption(_: &Toption) {}
460+
461+
// Using `if let Some(o) = topt.0` won't work here, as `borrow_toption` will try to borrow a
462+
// partially moved value
463+
if topt.0.is_some() {
464+
borrow_toption(&topt);
465+
topt.0.unwrap();
466+
//~^ unnecessary_unwrap
467+
}
468+
// This is fine though, as `if let Some(o) = &topt.0` won't (partially) move `topt`
469+
if topt.0.is_some() {
470+
borrow_toption(&topt);
471+
topt.0.as_ref().unwrap();
472+
//~^ unnecessary_unwrap
473+
}
439474
}

tests/ui/checked_unwrap/simple_conditionals.stderr

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,34 @@ LL | if result.is_ok() {
345345
LL | println!("{:?}", result.unwrap());
346346
| ^^^^^^^^^^^^^^^
347347

348+
error: called `unwrap` on `x` after checking its variant with `is_some`
349+
--> tests/ui/checked_unwrap/simple_conditionals.rs:299:9
350+
|
351+
LL | if x.is_some() {
352+
| -------------- help: try: `if let Some(<item>) = x`
353+
LL | borrow_option(&x);
354+
LL | x.unwrap();
355+
| ^^^^^^^^^^
356+
357+
error: called `unwrap` on `x` after checking its variant with `is_some`
358+
--> tests/ui/checked_unwrap/simple_conditionals.rs:305:9
359+
|
360+
LL | if x.is_some() {
361+
| -------------- help: try: `if let Some(<item>) = &x`
362+
LL | borrow_option(&x);
363+
LL | x.as_ref().unwrap();
364+
| ^^^^^^^^^^^^^^^^^^^
365+
348366
error: called `unwrap` on `sopt.option` after checking its variant with `is_some`
349-
--> tests/ui/checked_unwrap/simple_conditionals.rs:303:9
367+
--> tests/ui/checked_unwrap/simple_conditionals.rs:321:9
350368
|
351369
LL | let _res = if sopt.option.is_some() {
352370
| ------------------------ help: try: `if let Some(<item>) = sopt.option`
353371
LL | sopt.option.unwrap()
354372
| ^^^^^^^^^^^^^^^^^^^^
355373

356374
error: this call to `unwrap()` will always panic
357-
--> tests/ui/checked_unwrap/simple_conditionals.rs:306:9
375+
--> tests/ui/checked_unwrap/simple_conditionals.rs:324:9
358376
|
359377
LL | let _res = if sopt.option.is_some() {
360378
| --------------------- because of this check
@@ -363,7 +381,7 @@ LL | sopt.option.unwrap()
363381
| ^^^^^^^^^^^^^^^^^^^^
364382

365383
error: called `unwrap` on `sopt.option` after checking its variant with `is_some`
366-
--> tests/ui/checked_unwrap/simple_conditionals.rs:312:9
384+
--> tests/ui/checked_unwrap/simple_conditionals.rs:330:9
367385
|
368386
LL | let _res = if sopt.option.is_some() {
369387
| ------------------------ help: try: `if let Some(<item>) = sopt.option`
@@ -372,7 +390,7 @@ LL | sopt.option.unwrap()
372390
| ^^^^^^^^^^^^^^^^^^^^
373391

374392
error: this call to `unwrap()` will always panic
375-
--> tests/ui/checked_unwrap/simple_conditionals.rs:316:9
393+
--> tests/ui/checked_unwrap/simple_conditionals.rs:334:9
376394
|
377395
LL | let _res = if sopt.option.is_some() {
378396
| --------------------- because of this check
@@ -381,15 +399,15 @@ LL | sopt.option.unwrap()
381399
| ^^^^^^^^^^^^^^^^^^^^
382400

383401
error: called `unwrap` on `topt.0` after checking its variant with `is_some`
384-
--> tests/ui/checked_unwrap/simple_conditionals.rs:340:9
402+
--> tests/ui/checked_unwrap/simple_conditionals.rs:358:9
385403
|
386404
LL | let _res = if topt.0.is_some() {
387405
| ------------------- help: try: `if let Some(<item>) = topt.0`
388406
LL | topt.0.unwrap()
389407
| ^^^^^^^^^^^^^^^
390408

391409
error: this call to `unwrap()` will always panic
392-
--> tests/ui/checked_unwrap/simple_conditionals.rs:343:9
410+
--> tests/ui/checked_unwrap/simple_conditionals.rs:361:9
393411
|
394412
LL | let _res = if topt.0.is_some() {
395413
| ---------------- because of this check
@@ -398,7 +416,7 @@ LL | topt.0.unwrap()
398416
| ^^^^^^^^^^^^^^^
399417

400418
error: called `unwrap` on `topt.0` after checking its variant with `is_some`
401-
--> tests/ui/checked_unwrap/simple_conditionals.rs:349:9
419+
--> tests/ui/checked_unwrap/simple_conditionals.rs:367:9
402420
|
403421
LL | let _res = if topt.0.is_some() {
404422
| ------------------- help: try: `if let Some(<item>) = topt.0`
@@ -407,7 +425,7 @@ LL | topt.0.unwrap()
407425
| ^^^^^^^^^^^^^^^
408426

409427
error: this call to `unwrap()` will always panic
410-
--> tests/ui/checked_unwrap/simple_conditionals.rs:353:9
428+
--> tests/ui/checked_unwrap/simple_conditionals.rs:371:9
411429
|
412430
LL | let _res = if topt.0.is_some() {
413431
| ---------------- because of this check
@@ -416,15 +434,15 @@ LL | topt.0.unwrap()
416434
| ^^^^^^^^^^^^^^^
417435

418436
error: called `unwrap` on `sopt2.option.option` after checking its variant with `is_some`
419-
--> tests/ui/checked_unwrap/simple_conditionals.rs:387:9
437+
--> tests/ui/checked_unwrap/simple_conditionals.rs:405:9
420438
|
421439
LL | let _res = if sopt2.option.option.is_some() {
422440
| -------------------------------- help: try: `if let Some(<item>) = sopt2.option.option`
423441
LL | sopt2.option.option.unwrap()
424442
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
425443

426444
error: this call to `unwrap()` will always panic
427-
--> tests/ui/checked_unwrap/simple_conditionals.rs:390:9
445+
--> tests/ui/checked_unwrap/simple_conditionals.rs:408:9
428446
|
429447
LL | let _res = if sopt2.option.option.is_some() {
430448
| ----------------------------- because of this check
@@ -433,7 +451,7 @@ LL | sopt2.option.option.unwrap()
433451
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
434452

435453
error: called `unwrap` on `sopt2.option.option` after checking its variant with `is_some`
436-
--> tests/ui/checked_unwrap/simple_conditionals.rs:397:9
454+
--> tests/ui/checked_unwrap/simple_conditionals.rs:415:9
437455
|
438456
LL | let _res = if sopt2.option.option.is_some() {
439457
| -------------------------------- help: try: `if let Some(<item>) = sopt2.option.option`
@@ -442,7 +460,7 @@ LL | sopt2.option.option.unwrap()
442460
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
443461

444462
error: this call to `unwrap()` will always panic
445-
--> tests/ui/checked_unwrap/simple_conditionals.rs:401:9
463+
--> tests/ui/checked_unwrap/simple_conditionals.rs:419:9
446464
|
447465
LL | let _res = if sopt2.option.option.is_some() {
448466
| ----------------------------- because of this check
@@ -451,7 +469,7 @@ LL | sopt2.option.option.unwrap()
451469
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
452470

453471
error: called `unwrap` on `sopt2.option.option` after checking its variant with `is_some`
454-
--> tests/ui/checked_unwrap/simple_conditionals.rs:407:9
472+
--> tests/ui/checked_unwrap/simple_conditionals.rs:425:9
455473
|
456474
LL | let _res = if sopt2.option.option.is_some() {
457475
| -------------------------------- help: try: `if let Some(<item>) = sopt2.option.option`
@@ -460,13 +478,31 @@ LL | sopt2.option.option.unwrap()
460478
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
461479

462480
error: this call to `unwrap()` will always panic
463-
--> tests/ui/checked_unwrap/simple_conditionals.rs:411:9
481+
--> tests/ui/checked_unwrap/simple_conditionals.rs:429:9
464482
|
465483
LL | let _res = if sopt2.option.option.is_some() {
466484
| ----------------------------- because of this check
467485
...
468486
LL | sopt2.option.option.unwrap()
469487
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
470488

471-
error: aborting due to 53 previous errors
489+
error: called `unwrap` on `topt.0` after checking its variant with `is_some`
490+
--> tests/ui/checked_unwrap/simple_conditionals.rs:465:9
491+
|
492+
LL | if topt.0.is_some() {
493+
| ------------------- help: try: `if let Some(<item>) = topt.0`
494+
LL | borrow_toption(&topt);
495+
LL | topt.0.unwrap();
496+
| ^^^^^^^^^^^^^^^
497+
498+
error: called `unwrap` on `topt.0` after checking its variant with `is_some`
499+
--> tests/ui/checked_unwrap/simple_conditionals.rs:471:9
500+
|
501+
LL | if topt.0.is_some() {
502+
| ------------------- help: try: `if let Some(<item>) = &topt.0`
503+
LL | borrow_toption(&topt);
504+
LL | topt.0.as_ref().unwrap();
505+
| ^^^^^^^^^^^^^^^^^^^^^^^^
506+
507+
error: aborting due to 57 previous errors
472508

0 commit comments

Comments
 (0)