fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![warn(clippy::single_match)] 2#![allow( 3 unused, 4 clippy::uninlined_format_args, 5 clippy::needless_if, 6 clippy::redundant_guards, 7 clippy::redundant_pattern_matching 8)] 9fn dummy() {} 10 11fn single_match() { 12 let x = Some(1u8); 13 14 if let Some(y) = x { 15 println!("{:?}", y); 16 }; 17 18 let x = Some(1u8); 19 if let Some(y) = x { println!("{:?}", y) } 20 21 let z = (1u8, 1u8); 22 if let (2..=3, 7..=9) = z { dummy() }; 23 24 // Not linted (pattern guards used) 25 match x { 26 Some(y) if y == 0 => println!("{:?}", y), 27 _ => (), 28 } 29 30 // Not linted (no block with statements in the single arm) 31 match z { 32 (2..=3, 7..=9) => println!("{:?}", z), 33 _ => println!("nope"), 34 } 35} 36 37enum Foo { 38 Bar, 39 Baz(u8), 40} 41use std::borrow::Cow; 42use Foo::*; 43 44fn single_match_know_enum() { 45 let x = Some(1u8); 46 let y: Result<_, i8> = Ok(1i8); 47 48 if let Some(y) = x { dummy() }; 49 50 if let Ok(y) = y { dummy() }; 51 52 let c = Cow::Borrowed(""); 53 54 if let Cow::Borrowed(..) = c { dummy() }; 55 56 let z = Foo::Bar; 57 // no warning 58 match z { 59 Bar => println!("42"), 60 Baz(_) => (), 61 } 62 63 match z { 64 Baz(_) => println!("42"), 65 Bar => (), 66 } 67} 68 69// issue #173 70fn if_suggestion() { 71 let x = "test"; 72 if x == "test" { println!() } 73 74 #[derive(PartialEq, Eq)] 75 enum Foo { 76 A, 77 B, 78 C(u32), 79 } 80 81 let x = Foo::A; 82 if x == Foo::A { println!() } 83 84 const FOO_C: Foo = Foo::C(0); 85 if x == FOO_C { println!() } 86 87 if x == Foo::A { println!() } 88 89 let x = &x; 90 if x == &Foo::A { println!() } 91 92 enum Bar { 93 A, 94 B, 95 } 96 impl PartialEq for Bar { 97 fn eq(&self, rhs: &Self) -> bool { 98 matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B)) 99 } 100 } 101 impl Eq for Bar {} 102 103 let x = Bar::A; 104 if let Bar::A = x { println!() } 105 106 // issue #7038 107 struct X; 108 let x = Some(X); 109 if let None = x { println!() }; 110} 111 112// See: issue #8282 113fn ranges() { 114 enum E { 115 V, 116 } 117 let x = (Some(E::V), Some(42)); 118 119 // Don't lint, because the `E` enum can be extended with additional fields later. Thus, the 120 // proposed replacement to `if let Some(E::V)` may hide non-exhaustive warnings that appeared 121 // because of `match` construction. 122 match x { 123 (Some(E::V), _) => {}, 124 (None, _) => {}, 125 } 126 127 // lint 128 if let (Some(_), _) = x {} 129 130 // lint 131 if let (Some(E::V), _) = x { todo!() } 132 133 // lint 134 if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {} 135 136 // Don't lint, see above. 137 match (Some(E::V), Some(E::V), Some(E::V)) { 138 (.., Some(E::V), _) => {}, 139 (.., None, _) => {}, 140 } 141 142 // Don't lint, see above. 143 match (Some(E::V), Some(E::V), Some(E::V)) { 144 (Some(E::V), ..) => {}, 145 (None, ..) => {}, 146 } 147 148 // Don't lint, see above. 149 match (Some(E::V), Some(E::V), Some(E::V)) { 150 (_, Some(E::V), ..) => {}, 151 (_, None, ..) => {}, 152 } 153} 154 155fn skip_type_aliases() { 156 enum OptionEx { 157 Some(i32), 158 None, 159 } 160 enum ResultEx { 161 Err(i32), 162 Ok(i32), 163 } 164 165 use OptionEx::{None, Some}; 166 use ResultEx::{Err, Ok}; 167 168 // don't lint 169 match Err(42) { 170 Ok(_) => dummy(), 171 Err(_) => (), 172 }; 173 174 // don't lint 175 match Some(1i32) { 176 Some(_) => dummy(), 177 None => (), 178 }; 179} 180 181macro_rules! single_match { 182 ($num:literal) => { 183 match $num { 184 15 => println!("15"), 185 _ => (), 186 } 187 }; 188} 189 190fn main() { 191 single_match!(5); 192 193 // Don't lint 194 let _ = match Some(0) { 195 #[cfg(feature = "foo")] 196 Some(10) => 11, 197 Some(x) => x, 198 _ => 0, 199 }; 200} 201 202fn issue_10808(bar: Option<i32>) { 203 if let Some(v) = bar { unsafe { 204 let r = &v as *const i32; 205 println!("{}", *r); 206 } } 207 208 if let Some(v) = bar { 209 unsafe { 210 let r = &v as *const i32; 211 println!("{}", *r); 212 } 213 } 214} 215 216mod issue8634 { 217 struct SomeError(i32, i32); 218 219 fn foo(x: Result<i32, ()>) { 220 match x { 221 Ok(y) => { 222 println!("Yay! {y}"); 223 }, 224 Err(()) => { 225 // Ignore this error because blah blah blah. 226 }, 227 } 228 } 229 230 fn bar(x: Result<i32, SomeError>) { 231 match x { 232 Ok(y) => { 233 println!("Yay! {y}"); 234 }, 235 Err(_) => { 236 // TODO: Process the error properly. 237 }, 238 } 239 } 240 241 fn block_comment(x: Result<i32, SomeError>) { 242 match x { 243 Ok(y) => { 244 println!("Yay! {y}"); 245 }, 246 Err(_) => { 247 /* 248 let's make sure that this also 249 does not lint block comments. 250 */ 251 }, 252 } 253 } 254}