summaryrefslogtreecommitdiffstats
path: root/src/test/ui/closures/2229_closure_analysis/diagnostics/liveness_unintentional_copy.rs
blob: 465c9476ba65b1636532ab50c9b3650543242747 (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
// edition:2021

// check-pass
#![warn(unused)]
#![allow(dead_code)]

#[derive(Debug)]
struct MyStruct {
    a: i32,
    b: i32,
}

pub fn unintentional_copy_one() {
    let mut a = 1;
    let mut last = MyStruct{ a: 1, b: 1};
    let mut f = move |s| {
        // This will not trigger a warning for unused variable
        // as last.a will be treated as a Non-tracked place
        last.a = s;
        a = s;
        //~^ WARN value assigned to `a` is never read
        //~| WARN unused variable: `a`
    };
    f(2);
    f(3);
    f(4);
}

pub fn unintentional_copy_two() {
    let mut a = 1;
    let mut sum = MyStruct{ a: 1, b: 0};
    (1..10).for_each(move |x| {
        // This will not trigger a warning for unused variable
        // as sum.b will be treated as a Non-tracked place
        sum.b += x;
        a += x; //~ WARN unused variable: `a`
    });
}

fn main() {
    unintentional_copy_one();
    unintentional_copy_two();
}