summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/single_match_else.fixed
blob: f3b1de3b44f90999d97d2c9291afd127e2775f25 (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
//@aux-build: proc_macros.rs
#![warn(clippy::single_match_else)]
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
extern crate proc_macros;
use proc_macros::with_span;

enum ExprNode {
    ExprAddrOf,
    Butterflies,
    Unicorns,
}

static NODE: ExprNode = ExprNode::Unicorns;

fn unwrap_addr() -> Option<&'static ExprNode> {
    let _ = if let ExprNode::ExprAddrOf = ExprNode::Butterflies { Some(&NODE) } else {
        let x = 5;
        None
    };

    // Don't lint
    with_span!(span match ExprNode::Butterflies {
        ExprNode::ExprAddrOf => Some(&NODE),
        _ => {
            let x = 5;
            None
        },
    })
}

macro_rules! unwrap_addr {
    ($expression:expr) => {
        match $expression {
            ExprNode::ExprAddrOf => Some(&NODE),
            _ => {
                let x = 5;
                None
            },
        }
    };
}

#[rustfmt::skip]
fn main() {
    unwrap_addr!(ExprNode::Unicorns);

    //
    // don't lint single exprs/statements
    //

    // don't lint here
    match Some(1) {
        Some(a) => println!("${:?}", a),
        None => return,
    }

    // don't lint here
    match Some(1) {
        Some(a) => println!("${:?}", a),
        None => {
            return
        },
    }

    // don't lint here
    match Some(1) {
        Some(a) => println!("${:?}", a),
        None => {
            return;
        },
    }

    //
    // lint multiple exprs/statements "else" blocks
    //

    // lint here
    if let Some(a) = Some(1) { println!("${:?}", a) } else {
        println!("else block");
        return
    }

    // lint here
    if let Some(a) = Some(1) { println!("${:?}", a) } else {
        println!("else block");
        return;
    }

    // lint here
    use std::convert::Infallible;
    if let Ok(a) = Result::<i32, Infallible>::Ok(1) { println!("${:?}", a) } else {
        println!("else block");
        return;
    }

    use std::borrow::Cow;
    if let Cow::Owned(a) = Cow::from("moo") { println!("${:?}", a) } else {
        println!("else block");
        return;
    }
}

fn issue_10808(bar: Option<i32>) {
    if let Some(v) = bar { unsafe {
        let r = &v as *const i32;
        println!("{}", *r);
    } } else {
        println!("None1");
        println!("None2");
    }

    if let Some(v) = bar {
        println!("Some");
        println!("{v}");
    } else { unsafe {
        let v = 0;
        let r = &v as *const i32;
        println!("{}", *r);
    } }

    if let Some(v) = bar { unsafe {
        let r = &v as *const i32;
        println!("{}", *r);
    } } else { unsafe {
        let v = 0;
        let r = &v as *const i32;
        println!("{}", *r);
    } }

    if let Some(v) = bar {
        unsafe {
            let r = &v as *const i32;
            println!("{}", *r);
        }
    } else {
        println!("None");
        println!("None");
    }

    match bar {
        Some(v) => {
            println!("Some");
            println!("{v}");
        },
        #[rustfmt::skip]
        None => {
            unsafe {
                let v = 0;
                let r = &v as *const i32;
                println!("{}", *r);
            }
        },
    }

    match bar {
        #[rustfmt::skip]
        Some(v) => {
            unsafe {
                let r = &v as *const i32;
                println!("{}", *r);
            }
        },
        #[rustfmt::skip]
        None => {
            unsafe {
                let v = 0;
                let r = &v as *const i32;
                println!("{}", *r);
            }
        },
    }
}