2 #![warn(clippy::option_if_let_else)]
3 #![allow(clippy::redundant_closure, clippy::ref_option_ref, clippy::equatable_if_let)]
5 fn bad1(string: Option<&str>) -> (bool, &str) {
6 if let Some(x) = string {
13 fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
16 } else if let Some(x) = string {
23 fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
24 let _ = if let Some(s) = *string { s.len() } else { 0 };
25 let _ = if let Some(s) = &num { s } else { &0 };
26 let _ = if let Some(s) = &mut num {
32 let _ = if let Some(ref s) = num { s } else { &0 };
33 let _ = if let Some(mut s) = num {
39 let _ = if let Some(ref mut s) = num {
47 fn longer_body(arg: Option<u32>) -> u32 {
48 if let Some(x) = arg {
56 fn impure_else(arg: Option<i32>) {
57 let side_effect = || {
61 let _ = if let Some(x) = arg {
64 // map_or_else must be suggested
69 fn test_map_or_else(arg: Option<u32>) {
70 let _ = if let Some(x) = arg {
80 fn negative_tests(arg: Option<u32>) -> u32 {
81 let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
83 let _ = if let Some(x) = arg {
89 let _ = if let Some(x) = arg {
98 let optional = Some(5);
99 let _ = if let Some(x) = optional { x + 2 } else { 5 };
101 let _ = else_if_option(None);
102 unop_bad(&None, None);
103 let _ = longer_body(None);
104 test_map_or_else(None);
105 let _ = negative_tests(None);
106 let _ = impure_else(None);
108 let _ = if let Some(x) = Some(0) {
119 const fn _f(x: Option<u32>) -> u32 {
120 // Don't lint, `map_or` isn't const
121 if let Some(x) = x { x } else { 10 }
125 let s = String::new();
126 // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
127 let _ = if let Some(x) = Some(0) {
134 let s = String::new();
135 // Lint, both branches immutably borrow `s`.
136 let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
138 let s = String::new();
139 // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
140 let _ = if let Some(x) = Some(0) {
147 let s = Some(String::new());
148 // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
149 let _ = if let Some(x) = &s {
156 let mut s = Some(String::new());
157 // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
158 let _ = if let Some(x) = &mut s {
166 async fn _f1(x: u32) -> u32 {
171 // Don't lint. `await` can't be moved into a closure.
172 let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };