fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.5 KiB172 linesraw
1//@aux-build: proc_macros.rs
2#![warn(clippy::single_match_else)]
3#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
4extern crate proc_macros;
5use proc_macros::with_span;
6
7enum ExprNode {
8    ExprAddrOf,
9    Butterflies,
10    Unicorns,
11}
12
13static NODE: ExprNode = ExprNode::Unicorns;
14
15fn unwrap_addr() -> Option<&'static ExprNode> {
16    let _ = if let ExprNode::ExprAddrOf = ExprNode::Butterflies { Some(&NODE) } else {
17        let x = 5;
18        None
19    };
20
21    // Don't lint
22    with_span!(span match ExprNode::Butterflies {
23        ExprNode::ExprAddrOf => Some(&NODE),
24        _ => {
25            let x = 5;
26            None
27        },
28    })
29}
30
31macro_rules! unwrap_addr {
32    ($expression:expr) => {
33        match $expression {
34            ExprNode::ExprAddrOf => Some(&NODE),
35            _ => {
36                let x = 5;
37                None
38            },
39        }
40    };
41}
42
43#[rustfmt::skip]
44fn main() {
45    unwrap_addr!(ExprNode::Unicorns);
46
47    //
48    // don't lint single exprs/statements
49    //
50
51    // don't lint here
52    match Some(1) {
53        Some(a) => println!("${:?}", a),
54        None => return,
55    }
56
57    // don't lint here
58    match Some(1) {
59        Some(a) => println!("${:?}", a),
60        None => {
61            return
62        },
63    }
64
65    // don't lint here
66    match Some(1) {
67        Some(a) => println!("${:?}", a),
68        None => {
69            return;
70        },
71    }
72
73    //
74    // lint multiple exprs/statements "else" blocks
75    //
76
77    // lint here
78    if let Some(a) = Some(1) { println!("${:?}", a) } else {
79        println!("else block");
80        return
81    }
82
83    // lint here
84    if let Some(a) = Some(1) { println!("${:?}", a) } else {
85        println!("else block");
86        return;
87    }
88
89    // lint here
90    use std::convert::Infallible;
91    if let Ok(a) = Result::<i32, Infallible>::Ok(1) { println!("${:?}", a) } else {
92        println!("else block");
93        return;
94    }
95
96    use std::borrow::Cow;
97    if let Cow::Owned(a) = Cow::from("moo") { println!("${:?}", a) } else {
98        println!("else block");
99        return;
100    }
101}
102
103fn issue_10808(bar: Option<i32>) {
104    if let Some(v) = bar { unsafe {
105        let r = &v as *const i32;
106        println!("{}", *r);
107    } } else {
108        println!("None1");
109        println!("None2");
110    }
111
112    if let Some(v) = bar {
113        println!("Some");
114        println!("{v}");
115    } else { unsafe {
116        let v = 0;
117        let r = &v as *const i32;
118        println!("{}", *r);
119    } }
120
121    if let Some(v) = bar { unsafe {
122        let r = &v as *const i32;
123        println!("{}", *r);
124    } } else { unsafe {
125        let v = 0;
126        let r = &v as *const i32;
127        println!("{}", *r);
128    } }
129
130    if let Some(v) = bar {
131        unsafe {
132            let r = &v as *const i32;
133            println!("{}", *r);
134        }
135    } else {
136        println!("None");
137        println!("None");
138    }
139
140    match bar {
141        Some(v) => {
142            println!("Some");
143            println!("{v}");
144        },
145        #[rustfmt::skip]
146        None => {
147            unsafe {
148                let v = 0;
149                let r = &v as *const i32;
150                println!("{}", *r);
151            }
152        },
153    }
154
155    match bar {
156        #[rustfmt::skip]
157        Some(v) => {
158            unsafe {
159                let r = &v as *const i32;
160                println!("{}", *r);
161            }
162        },
163        #[rustfmt::skip]
164        None => {
165            unsafe {
166                let v = 0;
167                let r = &v as *const i32;
168                println!("{}", *r);
169            }
170        },
171    }
172}