about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/significant_drop_in_scrutinee.rs
blob: 78fc365bd5bb538ce43ac2adea84bac2def156f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// FIXME: Ideally these suggestions would be fixed via rustfix. Blocked by rust-lang/rust#53934
//@no-rustfix
#![warn(clippy::significant_drop_in_scrutinee)]
#![allow(dead_code, unused_assignments)]
#![allow(
    clippy::match_single_binding,
    clippy::single_match,
    clippy::uninlined_format_args,
    clippy::needless_lifetimes
)]

use std::num::ParseIntError;
use std::ops::Deref;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, MutexGuard, RwLock};

struct State {}

impl State {
    fn foo(&self) -> bool {
        true
    }

    fn bar(&self) {}
}

fn should_not_trigger_lint_with_mutex_guard_outside_match() {
    let mutex = Mutex::new(State {});

    // Should not trigger lint because the temporary should drop at the `;` on line before the match
    let is_foo = mutex.lock().unwrap().foo();
    match is_foo {
        true => {
            mutex.lock().unwrap().bar();
        },
        false => {},
    };
}

fn should_not_trigger_lint_with_mutex_guard_when_taking_ownership_in_match() {
    let mutex = Mutex::new(State {});

    // Should not trigger lint because the scrutinee is explicitly returning the MutexGuard,
    // so its lifetime should not be surprising.
    match mutex.lock() {
        Ok(guard) => {
            guard.foo();
            mutex.lock().unwrap().bar();
        },
        _ => {},
    };
}

fn should_trigger_lint_with_mutex_guard_in_match_scrutinee() {
    let mutex = Mutex::new(State {});

    // Should trigger lint because the lifetime of the temporary MutexGuard is surprising because it
    // is preserved until the end of the match, but there is no clear indication that this is the
    // case.
    match mutex.lock().unwrap().foo() {
        //~^ significant_drop_in_scrutinee
        true => {
            mutex.lock().unwrap().bar();
        },
        false => {},
    };
}

fn should_not_trigger_lint_with_mutex_guard_in_match_scrutinee_when_lint_allowed() {
    let mutex = Mutex::new(State {});

    // Lint should not be triggered because it is "allowed" below.
    #[allow(clippy::significant_drop_in_scrutinee)]
    match mutex.lock().unwrap().foo() {
        true => {
            mutex.lock().unwrap().bar();
        },
        false => {},
    };
}

fn should_not_trigger_lint_for_insignificant_drop() {
    // Should not trigger lint because there are no temporaries whose drops have a significant
    // side effect.
    match 1u64.to_string().is_empty() {
        true => {
            println!("It was empty")
        },
        false => {
            println!("It was not empty")
        },
    }
}

struct StateWithMutex {
    m: Mutex<u64>,
}

struct MutexGuardWrapper<'a> {
    mg: MutexGuard<'a, u64>,
}

impl<'a> MutexGuardWrapper<'a> {
    fn get_the_value(&self) -> u64 {
        *self.mg.deref()
    }
}

struct MutexGuardWrapperWrapper<'a> {
    mg: MutexGuardWrapper<'a>,
}

impl<'a> MutexGuardWrapperWrapper<'a> {
    fn get_the_value(&self) -> u64 {
        *self.mg.mg.deref()
    }
}

impl StateWithMutex {
    fn lock_m(&self) -> MutexGuardWrapper<'_> {
        MutexGuardWrapper {
            mg: self.m.lock().unwrap(),
        }
    }

    fn lock_m_m(&self) -> MutexGuardWrapperWrapper<'_> {
        MutexGuardWrapperWrapper {
            mg: MutexGuardWrapper {
                mg: self.m.lock().unwrap(),
            },
        }
    }

    fn foo(&self) -> bool {
        true
    }

    fn bar(&self) {}
}

fn should_trigger_lint_with_wrapped_mutex() {
    let s = StateWithMutex { m: Mutex::new(1) };

    // Should trigger lint because a temporary contains a type with a significant drop and its
    // lifetime is not obvious. Additionally, it is not obvious from looking at the scrutinee that
    // the temporary contains such a type, making it potentially even more surprising.
    match s.lock_m().get_the_value() {
        //~^ significant_drop_in_scrutinee
        1 => {
            println!("Got 1. Is it still 1?");
            println!("{}", s.lock_m().get_the_value());
        },
        2 => {
            println!("Got 2. Is it still 2?");
            println!("{}", s.lock_m().get_the_value());
        },
        _ => {},
    }
    println!("All done!");
}

fn should_trigger_lint_with_double_wrapped_mutex() {
    let s = StateWithMutex { m: Mutex::new(1) };

    // Should trigger lint because a temporary contains a type which further contains a type with a
    // significant drop and its lifetime is not obvious. Additionally, it is not obvious from
    // looking at the scrutinee that the temporary contains such a type, making it potentially even
    // more surprising.
    match s.lock_m_m().get_the_value() {
        //~^ significant_drop_in_scrutinee
        1 => {
            println!("Got 1. Is it still 1?");
            println!("{}", s.lock_m().get_the_value());
        },
        2 => {
            println!("Got 2. Is it still 2?");
            println!("{}", s.lock_m().get_the_value());
        },
        _ => {},
    }
    println!("All done!");
}

struct Counter {
    i: AtomicU64,
}

#[clippy::has_significant_drop]
struct CounterWrapper<'a> {
    counter: &'a Counter,
}

impl<'a> CounterWrapper<'a> {
    fn new(counter: &Counter) -> CounterWrapper<'_> {
        counter.i.fetch_add(1, Ordering::Relaxed);
        CounterWrapper { counter }
    }
}

impl<'a> Drop for CounterWrapper<'a> {
    fn drop(&mut self) {
        self.counter.i.fetch_sub(1, Ordering::Relaxed);
    }
}

impl Counter {
    fn temp_increment(&self) -> Vec<CounterWrapper<'_>> {
        vec![CounterWrapper::new(self), CounterWrapper::new(self)]
    }
}

fn should_trigger_lint_for_vec() {
    let counter = Counter { i: AtomicU64::new(0) };

    // Should trigger lint because the temporary in the scrutinee returns a collection of types
    // which have significant drops. The types with significant drops are also non-obvious when
    // reading the expression in the scrutinee.
    match counter.temp_increment().len() {
        //~^ significant_drop_in_scrutinee
        2 => {
            let current_count = counter.i.load(Ordering::Relaxed);
            println!("Current count {}", current_count);
            assert_eq!(current_count, 0);
        },
        1 => {},
        3 => {},
        _ => {},
    };
}

struct StateWithField {
    s: String,
}

// Should trigger lint only on the type in the tuple which is created using a temporary
// with a significant drop. Additionally, this test ensures that the format of the tuple
// is preserved correctly in the suggestion.
fn should_trigger_lint_for_tuple_in_scrutinee() {
    let mutex1 = Mutex::new(StateWithField { s: "one".to_owned() });

    {
        match (mutex1.lock().unwrap().s.len(), true) {
            //~^ significant_drop_in_scrutinee
            (3, _) => {
                println!("started");
                mutex1.lock().unwrap().s.len();
                println!("done");
            },
            (_, _) => {},
        };

        match (true, mutex1.lock().unwrap().s.len(), true) {
            //~^ significant_drop_in_scrutinee
            (_, 3, _) => {
                println!("started");
                mutex1.lock().unwrap().s.len();
                println!("done");
            },
            (_, _, _) => {},
        };

        let mutex2 = Mutex::new(StateWithField { s: "two".to_owned() });
        match (mutex1.lock().unwrap().s.len(), true, mutex2.lock().unwrap().s.len()) {
            //~^ significant_drop_in_scrutinee
            //~| significant_drop_in_scrutinee
            (3, _, 3) => {
                println!("started");
                mutex1.lock().unwrap().s.len();
                mutex2.lock().unwrap().s.len();
                println!("done");
            },
            (_, _, _) => {},
        };
    }
}

// Should not trigger lint since `String::as_str` returns a reference (i.e., `&str`)
// to the locked data (i.e., the `String`) and it is not surprising that matching such
// a reference needs to keep the data locked until the end of the match block.
fn should_not_trigger_lint_for_string_as_str() {
    let mutex1 = Mutex::new(StateWithField { s: "one".to_owned() });

    {
        let mutex2 = Mutex::new(StateWithField { s: "two".to_owned() });
        let mutex3 = Mutex::new(StateWithField { s: "three".to_owned() });

        match mutex3.lock().unwrap().s.as_str() {
            "three" => {
                println!("started");
                mutex1.lock().unwrap().s.len();
                mutex2.lock().unwrap().s.len();
                println!("done");
            },
            _ => {},
        };

        match (true, mutex3.lock().unwrap().s.as_str()) {
            (_, "three") => {
                println!("started");
                mutex1.lock().unwrap().s.len();
                mutex2.lock().unwrap().s.len();
                println!("done");
            },
            (_, _) => {},
        };
    }
}

// Should trigger lint when either side of a binary operation creates a temporary with a
// significant drop.
// To avoid potential unnecessary copies or creating references that would trigger the significant
// drop problem, the lint recommends moving the entire binary operation.
fn should_trigger_lint_for_accessing_field_in_mutex_in_one_side_of_binary_op() {
    let mutex = Mutex::new(StateWithField { s: "state".to_owned() });

    match mutex.lock().unwrap().s.len() > 1 {
        //~^ significant_drop_in_scrutinee
        true => {
            mutex.lock().unwrap().s.len();
        },
        false => {},
    };

    match 1 < mutex.lock().unwrap().s.len() {
        //~^ significant_drop_in_scrutinee
        true => {
            mutex.lock().unwrap().s.len();
        },
        false => {},
    };
}

// Should trigger lint when both sides of a binary operation creates a temporary with a
// significant drop.
// To avoid potential unnecessary copies or creating references that would trigger the significant
// drop problem, the lint recommends moving the entire binary operation.
fn should_trigger_lint_for_accessing_fields_in_mutex_in_both_sides_of_binary_op() {
    let mutex1 = Mutex::new(StateWithField { s: "state".to_owned() });
    let mutex2 = Mutex::new(StateWithField {
        s: "statewithfield".to_owned(),
    });

    match mutex1.lock().unwrap().s.len() < mutex2.lock().unwrap().s.len() {
        //~^ significant_drop_in_scrutinee
        //~| significant_drop_in_scrutinee
        true => {
            println!(
                "{} < {}",
                mutex1.lock().unwrap().s.len(),
                mutex2.lock().unwrap().s.len()
            );
        },
        false => {},
    };

    match mutex1.lock().unwrap().s.len() >= mutex2.lock().unwrap().s.len() {
        //~^ significant_drop_in_scrutinee
        //~| significant_drop_in_scrutinee
        true => {
            println!(
                "{} >= {}",
                mutex1.lock().unwrap().s.len(),
                mutex2.lock().unwrap().s.len()
            );
        },
        false => {},
    };
}

fn should_not_trigger_lint_for_closure_in_scrutinee() {
    let mutex1 = Mutex::new(StateWithField { s: "one".to_owned() });

    let get_mutex_guard = || mutex1.lock().unwrap().s.len();

    // Should not trigger lint because the temporary with a significant drop will be dropped
    // at the end of the closure, so the MutexGuard will be unlocked and not have a potentially
    // surprising lifetime.
    match get_mutex_guard() > 1 {
        true => {
            mutex1.lock().unwrap().s.len();
        },
        false => {},
    };
}

fn should_trigger_lint_for_return_from_closure_in_scrutinee() {
    let mutex1 = Mutex::new(StateWithField { s: "one".to_owned() });

    let get_mutex_guard = || mutex1.lock().unwrap();

    // Should trigger lint because the temporary with a significant drop is returned from the
    // closure but not used directly in any match arms, so it has a potentially surprising lifetime.
    match get_mutex_guard().s.len() > 1 {
        //~^ significant_drop_in_scrutinee
        true => {
            mutex1.lock().unwrap().s.len();
        },
        false => {},
    };
}

fn should_trigger_lint_for_return_from_match_in_scrutinee() {
    let mutex1 = Mutex::new(StateWithField { s: "one".to_owned() });
    let mutex2 = Mutex::new(StateWithField { s: "two".to_owned() });

    let i = 100;

    // Should trigger lint because the nested match within the scrutinee returns a temporary with a
    // significant drop is but not used directly in any match arms, so it has a potentially
    // surprising lifetime.
    match match i {
        //~^ significant_drop_in_scrutinee
        100 => mutex1.lock().unwrap(),
        _ => mutex2.lock().unwrap(),
    }
    .s
    .len()
        > 1
    {
        true => {
            mutex1.lock().unwrap().s.len();
        },
        false => {
            println!("nothing to do here");
        },
    };
}

fn should_trigger_lint_for_return_from_if_in_scrutinee() {
    let mutex1 = Mutex::new(StateWithField { s: "one".to_owned() });
    let mutex2 = Mutex::new(StateWithField { s: "two".to_owned() });

    let i = 100;

    // Should trigger lint because the nested if-expression within the scrutinee returns a temporary
    // with a significant drop is but not used directly in any match arms, so it has a potentially
    // surprising lifetime.
    match if i > 1 {
        //~^ significant_drop_in_scrutinee
        mutex1.lock().unwrap()
    } else {
        mutex2.lock().unwrap()
    }
    .s
    .len()
        > 1
    {
        true => {
            mutex1.lock().unwrap().s.len();
        },
        false => {},
    };
}

fn should_not_trigger_lint_for_if_in_scrutinee() {
    let mutex = Mutex::new(StateWithField { s: "state".to_owned() });

    let i = 100;

    // Should not trigger the lint because the temporary with a significant drop *is* dropped within
    // the body of the if-expression nested within the match scrutinee, and therefore does not have
    // a potentially surprising lifetime.
    match if i > 1 {
        mutex.lock().unwrap().s.len() > 1
    } else {
        false
    } {
        true => {
            mutex.lock().unwrap().s.len();
        },
        false => {},
    };
}

struct StateWithBoxedMutexGuard {
    u: Mutex<u64>,
}

impl StateWithBoxedMutexGuard {
    fn new() -> StateWithBoxedMutexGuard {
        StateWithBoxedMutexGuard { u: Mutex::new(42) }
    }
    fn lock(&self) -> Box<MutexGuard<'_, u64>> {
        Box::new(self.u.lock().unwrap())
    }
}

fn should_trigger_lint_for_boxed_mutex_guard() {
    let s = StateWithBoxedMutexGuard::new();

    // Should trigger lint because a temporary Box holding a type with a significant drop in a match
    // scrutinee may have a potentially surprising lifetime.
    match s.lock().deref().deref() {
        //~^ significant_drop_in_scrutinee
        0 | 1 => println!("Value was less than 2"),
        _ => println!("Value is {}", s.lock().deref()),
    };
}

struct StateStringWithBoxedMutexGuard {
    s: Mutex<String>,
}

impl StateStringWithBoxedMutexGuard {
    fn new() -> StateStringWithBoxedMutexGuard {
        StateStringWithBoxedMutexGuard {
            s: Mutex::new("A String".to_owned()),
        }
    }
    fn lock(&self) -> Box<MutexGuard<'_, String>> {
        Box::new(self.s.lock().unwrap())
    }
}

fn should_not_trigger_lint_for_string_ref() {
    let s = StateStringWithBoxedMutexGuard::new();

    let matcher = String::from("A String");

    // Should not trigger lint because the second `deref` returns a string reference (`&String`).
    // So it is not surprising that matching the reference implies that the lock needs to be held
    // until the end of the block.
    match s.lock().deref().deref() {
        matcher => println!("Value is {}", s.lock().deref()),
        _ => println!("Value was not a match"),
    };
}

struct StateWithIntField {
    i: u64,
}

// Should trigger lint when either side of an assign expression contains a temporary with a
// significant drop, because the temporary's lifetime will be extended to the end of the match.
// To avoid potential unnecessary copies or creating references that would trigger the significant
// drop problem, the lint recommends moving the entire binary operation.
fn should_trigger_lint_in_assign_expr() {
    let mutex = Mutex::new(StateWithIntField { i: 10 });

    let mut i = 100;

    match mutex.lock().unwrap().i = i {
        //~^ significant_drop_in_scrutinee
        _ => {
            println!("{}", mutex.lock().unwrap().i);
        },
    };

    match i = mutex.lock().unwrap().i {
        //~^ significant_drop_in_scrutinee
        _ => {
            println!("{}", mutex.lock().unwrap().i);
        },
    };

    match mutex.lock().unwrap().i += 1 {
        //~^ significant_drop_in_scrutinee
        _ => {
            println!("{}", mutex.lock().unwrap().i);
        },
    };

    match i += mutex.lock().unwrap().i {
        //~^ significant_drop_in_scrutinee
        _ => {
            println!("{}", mutex.lock().unwrap().i);
        },
    };
}

#[derive(Debug)]
enum RecursiveEnum {
    Foo(Option<Box<RecursiveEnum>>),
}

#[derive(Debug)]
enum GenericRecursiveEnum<T> {
    Foo(T, Option<Box<GenericRecursiveEnum<T>>>),
}

fn should_not_cause_stack_overflow() {
    // Test that when a type recursively contains itself, a stack overflow does not occur when
    // checking sub-types for significant drops.
    let f = RecursiveEnum::Foo(Some(Box::new(RecursiveEnum::Foo(None))));
    match f {
        RecursiveEnum::Foo(Some(f)) => {
            println!("{:?}", f)
        },
        RecursiveEnum::Foo(f) => {
            println!("{:?}", f)
        },
    }

    let f = GenericRecursiveEnum::Foo(1u64, Some(Box::new(GenericRecursiveEnum::Foo(2u64, None))));
    match f {
        GenericRecursiveEnum::Foo(i, Some(f)) => {
            println!("{} {:?}", i, f)
        },
        GenericRecursiveEnum::Foo(i, f) => {
            println!("{} {:?}", i, f)
        },
    }
}

fn should_not_produce_lint_for_try_desugar() -> Result<u64, ParseIntError> {
    // TryDesugar (i.e. using `?` for a Result type) will turn into a match but is out of scope
    // for this lint
    let rwlock = RwLock::new("1".to_string());
    let result = rwlock.read().unwrap().parse::<u64>()?;
    println!("{}", result);
    rwlock.write().unwrap().push('2');
    Ok(result)
}

struct ResultReturner {
    s: String,
}

impl ResultReturner {
    fn to_number(&self) -> Result<i64, ParseIntError> {
        self.s.parse::<i64>()
    }
}

fn should_trigger_lint_for_non_ref_move_and_clone_suggestion() {
    let rwlock = RwLock::<ResultReturner>::new(ResultReturner { s: "1".to_string() });
    match rwlock.read().unwrap().to_number() {
        //~^ significant_drop_in_scrutinee
        Ok(n) => println!("Converted to number: {}", n),
        Err(e) => println!("Could not convert {} to number", e),
    };
}

fn should_not_trigger_lint_for_read_write_lock_for_loop() {
    let rwlock = RwLock::<Vec<String>>::new(vec!["1".to_string()]);
    // Should not trigger lint. Since we're iterating over the data, it's obvious that the lock
    // has to be held until the iteration finishes.
    // https://github.com/rust-lang/rust-clippy/issues/8987
    for s in rwlock.read().unwrap().iter() {
        println!("{}", s);
    }
}

fn do_bar(mutex: &Mutex<State>) {
    mutex.lock().unwrap().bar();
}

fn should_trigger_lint_without_significant_drop_in_arm() {
    let mutex = Mutex::new(State {});

    // Should trigger lint because the lifetime of the temporary MutexGuard is surprising because it
    // is preserved until the end of the match, but there is no clear indication that this is the
    // case.
    match mutex.lock().unwrap().foo() {
        //~^ significant_drop_in_scrutinee
        true => do_bar(&mutex),
        false => {},
    };
}

fn should_not_trigger_on_significant_iterator_drop() {
    let lines = std::io::stdin().lines();
    for line in lines {
        println!("foo: {}", line.unwrap());
    }
}

// https://github.com/rust-lang/rust-clippy/issues/9072
fn should_not_trigger_lint_if_place_expr_has_significant_drop() {
    let x = Mutex::new(vec![1, 2, 3]);
    let x_guard = x.lock().unwrap();

    match x_guard[0] {
        1 => println!("1!"),
        x => println!("{x}"),
    }

    match x_guard.len() {
        1 => println!("1!"),
        x => println!("{x}"),
    }
}

struct Guard<'a, T>(MutexGuard<'a, T>);

struct Ref<'a, T>(&'a T);

impl<'a, T> Guard<'a, T> {
    fn guard(&self) -> &MutexGuard<'_, T> {
        &self.0
    }

    fn guard_ref(&self) -> Ref<'_, MutexGuard<'_, T>> {
        Ref(&self.0)
    }

    fn take(self) -> Box<MutexGuard<'a, T>> {
        Box::new(self.0)
    }
}

fn should_not_trigger_for_significant_drop_ref() {
    let mutex = Mutex::new(vec![1, 2]);
    let guard = Guard(mutex.lock().unwrap());

    match guard.guard().len() {
        0 => println!("empty"),
        _ => println!("not empty"),
    }

    match guard.guard_ref().0.len() {
        0 => println!("empty"),
        _ => println!("not empty"),
    }

    match guard.take().len() {
        //~^ significant_drop_in_scrutinee
        0 => println!("empty"),
        _ => println!("not empty"),
    };
}

struct Foo<'a>(&'a Vec<u32>);

impl<'a> Foo<'a> {
    fn copy_old_lifetime(&self) -> &'a Vec<u32> {
        self.0
    }

    fn reborrow_new_lifetime(&self) -> &Vec<u32> {
        self.0
    }
}

fn should_trigger_lint_if_and_only_if_lifetime_is_irrelevant() {
    let vec = Vec::new();
    let mutex = Mutex::new(Foo(&vec));

    // Should trigger lint even if `copy_old_lifetime()` has a lifetime, as the lifetime of
    // `&vec` is unrelated to the temporary with significant drop (i.e., the `MutexGuard`).
    for val in mutex.lock().unwrap().copy_old_lifetime() {
        //~^ significant_drop_in_scrutinee
        println!("{}", val);
    }

    // Should not trigger lint because `reborrow_new_lifetime()` has a lifetime and the
    // lifetime is related to the temporary with significant drop (i.e., the `MutexGuard`).
    for val in mutex.lock().unwrap().reborrow_new_lifetime() {
        println!("{}", val);
    }
}

fn should_not_trigger_lint_for_complex_lifetime() {
    let mutex = Mutex::new(vec!["hello".to_owned()]);
    let string = "world".to_owned();

    // Should not trigger lint due to the relevant lifetime.
    for c in mutex.lock().unwrap().first().unwrap().chars() {
        println!("{}", c);
    }

    // Should trigger lint due to the irrelevant lifetime.
    //
    // FIXME: The lifetime is too complex to analyze. In order to avoid false positives, we do not
    // trigger lint.
    for c in mutex.lock().unwrap().first().map(|_| &string).unwrap().chars() {
        println!("{}", c);
    }
}

fn should_not_trigger_lint_with_explicit_drop() {
    let mutex = Mutex::new(vec![1]);

    // Should not trigger lint since the drop explicitly happens.
    for val in [drop(mutex.lock().unwrap()), ()] {
        println!("{:?}", val);
    }

    // Should trigger lint if there is no explicit drop.
    for val in [mutex.lock().unwrap()[0], 2] {
        //~^ significant_drop_in_scrutinee
        println!("{:?}", val);
    }
}

fn should_trigger_lint_in_if_let() {
    let mutex = Mutex::new(vec![1]);

    if let Some(val) = mutex.lock().unwrap().first().copied() {
        //~^ significant_drop_in_scrutinee
        println!("{}", val);
    }

    // Should not trigger lint without the final `copied()`, because we actually hold a reference
    // (i.e., the `val`) to the locked data.
    if let Some(val) = mutex.lock().unwrap().first() {
        println!("{}", val);
    };
}

fn should_trigger_lint_in_while_let() {
    let mutex = Mutex::new(vec![1]);

    while let Some(val) = mutex.lock().unwrap().pop() {
        //~^ significant_drop_in_scrutinee
        println!("{}", val);
    }
}

async fn foo_async(mutex: &Mutex<i32>) -> Option<MutexGuard<'_, i32>> {
    Some(mutex.lock().unwrap())
}

async fn should_trigger_lint_for_async(mutex: Mutex<i32>) -> i32 {
    match *foo_async(&mutex).await.unwrap() {
        //~^ significant_drop_in_scrutinee
        n if n < 10 => n,
        _ => 10,
    }
}

async fn should_not_trigger_lint_in_async_expansion(mutex: Mutex<i32>) -> i32 {
    match foo_async(&mutex).await {
        Some(guard) => *guard,
        _ => 0,
    }
}

fn should_trigger_lint_in_match_expr() {
    let mutex = Mutex::new(State {});

    // Should trigger lint because the lifetime of the temporary MutexGuard is surprising because it
    // is preserved until the end of the match, but there is no clear indication that this is the
    // case.
    let _ = match mutex.lock().unwrap().foo() {
        //~^ significant_drop_in_scrutinee
        true => 0,
        false => 1,
    };
}

fn main() {}