summaryrefslogtreecommitdiffstats
path: root/tests/ui/try-block/try-block-unreachable-code-lint.rs
blob: e1d82ea360dc6294866dd78072a438882771e026 (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
// Test unreachable_code lint for `try {}` block ok-wrapping. See issues #54165, #63324.

// compile-flags: --edition 2018
// check-pass
#![feature(try_blocks)]
#![warn(unreachable_code)]

fn err() -> Result<u32, ()> {
    Err(())
}

// In the following cases unreachable code is autogenerated and should not be reported.

fn test_ok_wrapped_divergent_expr_1() {
    let res: Result<u32, ()> = try {
        loop {
            err()?;
        }
    };
    println!("res: {:?}", res);
}

fn test_ok_wrapped_divergent_expr_2() {
    let _: Result<u32, ()> = try {
        return
    };
}

fn test_autogenerated_unit_after_divergent_expr() {
    let _: Result<(), ()> = try {
        return;
    };
}

// In the following cases unreachable code should be reported.

fn test_try_block_after_divergent_stmt() {
    let _: Result<u32, ()> = {
        return;

        try {
            loop {
                err()?;
            }
        }
        //~^^^^^ WARNING unreachable expression
    };
}

fn test_wrapped_divergent_expr() {
    let _: Result<u32, ()> = {
        Err(return)
        //~^ WARNING unreachable call
    };
}

fn test_expr_after_divergent_stmt_in_try_block() {
    let res: Result<u32, ()> = try {
        loop {
            err()?;
        }

        42
        //~^ WARNING unreachable expression
    };
    println!("res: {:?}", res);
}

fn main() {
    test_ok_wrapped_divergent_expr_1();
    test_ok_wrapped_divergent_expr_2();
    test_autogenerated_unit_after_divergent_expr();
    test_try_block_after_divergent_stmt();
    test_wrapped_divergent_expr();
    test_expr_after_divergent_stmt_in_try_block();
}