summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/unnecessary_literal_unwrap.rs
blob: 7bd8deea4d1b3cf6b84b6f1907d3fa938fd6921a (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
#![warn(clippy::unnecessary_literal_unwrap)]
#![allow(unreachable_code)]
#![allow(
    clippy::unnecessary_lazy_evaluations,
    clippy::diverging_sub_expression,
    clippy::let_unit_value,
    clippy::no_effect
)]

fn unwrap_option_some() {
    let _val = Some(1).unwrap();
    let _val = Some(1).expect("this never happens");

    Some(1).unwrap();
    Some(1).expect("this never happens");
}

#[rustfmt::skip] // force rustfmt not to remove braces in `|| { 234 }`
fn unwrap_option_none() {
    let _val = None::<()>.unwrap();
    let _val = None::<()>.expect("this always happens");
    let _val: String = None.unwrap_or_default();
    let _val: u16 = None.unwrap_or(234);
    let _val: u16 = None.unwrap_or_else(|| 234);
    let _val: u16 = None.unwrap_or_else(|| { 234 });
    let _val: u16 = None.unwrap_or_else(|| -> u16 { 234 });

    None::<()>.unwrap();
    None::<()>.expect("this always happens");
    None::<String>.unwrap_or_default();
    None::<u16>.unwrap_or(234);
    None::<u16>.unwrap_or_else(|| 234);
    None::<u16>.unwrap_or_else(|| { 234 });
    None::<u16>.unwrap_or_else(|| -> u16 { 234 });
}

fn unwrap_result_ok() {
    let _val = Ok::<_, ()>(1).unwrap();
    let _val = Ok::<_, ()>(1).expect("this never happens");
    let _val = Ok::<_, ()>(1).unwrap_err();
    let _val = Ok::<_, ()>(1).expect_err("this always happens");

    Ok::<_, ()>(1).unwrap();
    Ok::<_, ()>(1).expect("this never happens");
    Ok::<_, ()>(1).unwrap_err();
    Ok::<_, ()>(1).expect_err("this always happens");
}

fn unwrap_result_err() {
    let _val = Err::<(), _>(1).unwrap_err();
    let _val = Err::<(), _>(1).expect_err("this never happens");
    let _val = Err::<(), _>(1).unwrap();
    let _val = Err::<(), _>(1).expect("this always happens");

    Err::<(), _>(1).unwrap_err();
    Err::<(), _>(1).expect_err("this never happens");
    Err::<(), _>(1).unwrap();
    Err::<(), _>(1).expect("this always happens");
}

fn unwrap_methods_option() {
    let _val = Some(1).unwrap_or(2);
    let _val = Some(1).unwrap_or_default();
    let _val = Some(1).unwrap_or_else(|| 2);

    Some(1).unwrap_or(2);
    Some(1).unwrap_or_default();
    Some(1).unwrap_or_else(|| 2);
}

fn unwrap_methods_result() {
    let _val = Ok::<_, ()>(1).unwrap_or(2);
    let _val = Ok::<_, ()>(1).unwrap_or_default();
    let _val = Ok::<_, ()>(1).unwrap_or_else(|_| 2);

    Ok::<_, ()>(1).unwrap_or(2);
    Ok::<_, ()>(1).unwrap_or_default();
    Ok::<_, ()>(1).unwrap_or_else(|_| 2);
}

fn unwrap_from_binding() {
    macro_rules! from_macro {
        () => {
            Some("")
        };
    }
    let val = from_macro!();
    let _ = val.unwrap_or("");
}

fn unwrap_unchecked() {
    let _ = unsafe { Some(1).unwrap_unchecked() };
    let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
    let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
    let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
    let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
    let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
    let _ = unsafe { Err::<(), i32>(123).unwrap_err_unchecked() };
}

fn main() {
    unwrap_option_some();
    unwrap_option_none();
    unwrap_result_ok();
    unwrap_result_err();
    unwrap_methods_option();
    unwrap_methods_result();
    unwrap_unchecked();
}