]> git.lizzy.rs Git - rust.git/blob - tests/ui/single_match_else.rs
Don't suggest let else in match if the else arm explicitly mentions non obvious paths
[rust.git] / tests / ui / single_match_else.rs
1 // aux-build: proc_macro_with_span.rs
2 #![warn(clippy::single_match_else)]
3 #![allow(clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
4
5 extern crate proc_macro_with_span;
6 use proc_macro_with_span::with_span;
7
8 enum ExprNode {
9     ExprAddrOf,
10     Butterflies,
11     Unicorns,
12 }
13
14 static NODE: ExprNode = ExprNode::Unicorns;
15
16 fn unwrap_addr() -> Option<&'static ExprNode> {
17     let _ = match ExprNode::Butterflies {
18         ExprNode::ExprAddrOf => Some(&NODE),
19         _ => {
20             let x = 5;
21             None
22         },
23     };
24
25     // Don't lint
26     with_span!(span match ExprNode::Butterflies {
27         ExprNode::ExprAddrOf => Some(&NODE),
28         _ => {
29             let x = 5;
30             None
31         },
32     })
33 }
34
35 macro_rules! unwrap_addr {
36     ($expression:expr) => {
37         match $expression {
38             ExprNode::ExprAddrOf => Some(&NODE),
39             _ => {
40                 let x = 5;
41                 None
42             },
43         }
44     };
45 }
46
47 #[rustfmt::skip]
48 fn main() {
49     unwrap_addr!(ExprNode::Unicorns);
50
51     //
52     // don't lint single exprs/statements
53     //
54
55     // don't lint here
56     match Some(1) {
57         Some(a) => println!("${:?}", a),
58         None => return,
59     }
60
61     // don't lint here
62     match Some(1) {
63         Some(a) => println!("${:?}", a),
64         None => {
65             return
66         },
67     }
68
69     // don't lint here
70     match Some(1) {
71         Some(a) => println!("${:?}", a),
72         None => {
73             return;
74         },
75     }
76
77     //
78     // lint multiple exprs/statements "else" blocks
79     //
80
81     // lint here
82     match Some(1) {
83         Some(a) => println!("${:?}", a),
84         None => {
85             println!("else block");
86             return
87         },
88     }
89
90     // lint here
91     match Some(1) {
92         Some(a) => println!("${:?}", a),
93         None => {
94             println!("else block");
95             return;
96         },
97     }
98
99     // lint here
100     use std::convert::Infallible;
101     match Result::<i32, Infallible>::Ok(1) {
102         Ok(a) => println!("${:?}", a),
103         Err(_) => {
104             println!("else block");
105             return;
106         }
107     }
108
109     use std::borrow::Cow;
110     match Cow::from("moo") {
111         Cow::Owned(a) => println!("${:?}", a),
112         Cow::Borrowed(_) => {
113             println!("else block");
114             return;
115         }
116     }
117 }