3 #![warn(clippy::option_if_let_else)]
4 #![allow(clippy::redundant_closure)]
5 #![allow(clippy::ref_option_ref)]
7 fn bad1(string: Option<&str>) -> (bool, &str) {
8 if let Some(x) = string {
15 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
18 } else if let Some(x) = string {
25 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
26 let _ = if let Some(s) = *string { s.len() } else { 0 };
27 let _ = if let Some(s) = &num { s } else { &0 };
28 let _ = if let Some(s) = &mut num {
34 let _ = if let Some(ref s) = num { s } else { &0 };
35 let _ = if let Some(mut s) = num {
41 let _ = if let Some(ref mut s) = num {
49 fn longer_body(arg: Option<u32>) -> u32 {
50 if let Some(x) = arg {
58 fn impure_else(arg: Option<i32>) {
59 let side_effect = || {
63 let _ = if let Some(x) = arg {
66 // map_or_else must be suggested
71 fn test_map_or_else(arg: Option<u32>) {
72 let _ = if let Some(x) = arg {
82 fn negative_tests(arg: Option<u32>) -> u32 {
83 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
85 let _ = if let Some(x) = arg {
91 let _ = if let Some(x) = arg {
100 let optional = Some(5);
101 let _ = if let Some(x) = optional { x + 2 } else { 5 };
103 let _ = else_if_option(None);
104 unop_bad(&None, None);
105 let _ = longer_body(None);
106 test_map_or_else(None);
107 let _ = negative_tests(None);
108 let _ = impure_else(None);
110 let _ = if let Some(x) = Some(0) {
121 const fn _f(x: Option<u32>) -> u32 {
122 // Don't lint, `map_or` isn't const
123 if let Some(x) = x { x } else { 10 }
127 let s = String::new();
128 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
129 let _ = if let Some(x) = Some(0) {
136 let s = String::new();
137 // Lint, both branches immutably borrow `s`.
138 let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
140 let s = String::new();
141 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
142 let _ = if let Some(x) = Some(0) {
149 let s = Some(String::new());
150 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
151 let _ = if let Some(x) = &s {
158 let mut s = Some(String::new());
159 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
160 let _ = if let Some(x) = &mut s {
168 async fn _f1(x: u32) -> u32 {
173 // Don't lint. `await` can't be moved into a closure.
174 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };