summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/redundant_pattern_matching_option.fixed
blob: c9b76262d70b4571c3b36b21fa23be667e37fefe (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
#![warn(clippy::all)]
#![warn(clippy::redundant_pattern_matching)]
#![allow(
    unused_must_use,
    clippy::needless_bool,
    clippy::needless_if,
    clippy::match_like_matches_macro,
    clippy::equatable_if_let,
    clippy::if_same_then_else
)]
#![feature(let_chains, if_let_guard)]

fn issue_11174<T>(boolean: bool, maybe_some: Option<T>) -> bool {
    maybe_some.is_none() && (!boolean)
}

fn issue_11174_edge_cases<T>(boolean: bool, boolean2: bool, maybe_some: Option<T>) {
    let _ = maybe_some.is_none() && (boolean || boolean2); // guard needs parentheses
    let _ = match maybe_some {
        // can't use `matches!` here
        // because `expr` metavars in macros don't allow let exprs
        None if let Some(x) = Some(0)
            && x > 5 =>
        {
            true
        },
        _ => false,
    };
}

fn main() {
    if None::<()>.is_none() {}

    if Some(42).is_some() {}

    if Some(42).is_some() {
        foo();
    } else {
        bar();
    }

    while Some(42).is_some() {}

    while Some(42).is_none() {}

    while None::<()>.is_none() {}

    let mut v = vec![1, 2, 3];
    while v.pop().is_some() {
        foo();
    }

    if None::<i32>.is_none() {}

    if Some(42).is_some() {}

    Some(42).is_some();

    None::<()>.is_none();

    let _ = None::<()>.is_none();

    let opt = Some(false);
    let _ = if opt.is_some() { true } else { false };

    issue6067();
    issue10726();
    issue10803();

    let _ = if gen_opt().is_some() {
        1
    } else if gen_opt().is_none() {
        2
    } else {
        3
    };

    if gen_opt().is_some() {}
}

fn gen_opt() -> Option<()> {
    None
}

fn foo() {}

fn bar() {}

// Methods that are unstable const should not be suggested within a const context, see issue #5697.
// However, in Rust 1.48.0 the methods `is_some` and `is_none` of `Option` were stabilized as const,
// so the following should be linted.
const fn issue6067() {
    if Some(42).is_some() {}

    if None::<()>.is_none() {}

    while Some(42).is_some() {}

    while None::<()>.is_none() {}

    Some(42).is_some();

    None::<()>.is_none();
}

#[allow(clippy::deref_addrof, dead_code, clippy::needless_borrow)]
fn issue7921() {
    if (&None::<()>).is_none() {}
    if (&None::<()>).is_none() {}
}

fn issue10726() {
    let x = Some(42);

    x.is_some();

    x.is_none();

    x.is_none();

    x.is_some();

    // Don't lint
    match x {
        Some(21) => true,
        _ => false,
    };
}

fn issue10803() {
    let x = Some(42);

    let _ = x.is_some();

    let _ = x.is_none();

    // Don't lint
    let _ = matches!(x, Some(16));
}