2020-03-05 12:35:05 -06:00
|
|
|
// run-rustfix
|
|
|
|
#![warn(clippy::option_if_let_else)]
|
2020-08-21 17:43:04 -05:00
|
|
|
#![allow(clippy::redundant_closure)]
|
2020-10-13 15:47:46 -05:00
|
|
|
#![allow(clippy::ref_option_ref)]
|
2020-03-05 12:35:05 -06:00
|
|
|
|
|
|
|
fn bad1(string: Option<&str>) -> (bool, &str) {
|
|
|
|
string.map_or((false, "hello"), |x| (true, x))
|
|
|
|
}
|
|
|
|
|
2020-04-25 11:08:23 -05:00
|
|
|
fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
|
2020-04-25 10:32:33 -05:00
|
|
|
if string.is_none() {
|
|
|
|
None
|
2021-05-13 04:50:25 -05:00
|
|
|
} else if let Some(x) = string {
|
|
|
|
Some((true, x))
|
|
|
|
} else {
|
|
|
|
Some((false, ""))
|
|
|
|
}
|
2020-04-25 10:32:33 -05:00
|
|
|
}
|
|
|
|
|
2020-05-09 22:20:57 -05:00
|
|
|
fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
|
|
|
|
let _ = string.map_or(0, |s| s.len());
|
|
|
|
let _ = num.as_ref().map_or(&0, |s| s);
|
|
|
|
let _ = num.as_mut().map_or(&mut 0, |s| {
|
|
|
|
*s += 1;
|
|
|
|
s
|
|
|
|
});
|
|
|
|
let _ = num.as_ref().map_or(&0, |s| s);
|
|
|
|
let _ = num.map_or(0, |mut s| {
|
|
|
|
s += 1;
|
|
|
|
s
|
|
|
|
});
|
|
|
|
let _ = num.as_mut().map_or(&mut 0, |s| {
|
|
|
|
*s += 1;
|
|
|
|
s
|
|
|
|
});
|
2020-04-25 11:08:23 -05:00
|
|
|
}
|
|
|
|
|
2020-03-05 12:35:05 -06:00
|
|
|
fn longer_body(arg: Option<u32>) -> u32 {
|
|
|
|
arg.map_or(13, |x| {
|
|
|
|
let y = x * x;
|
|
|
|
y * y
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-21 17:43:04 -05:00
|
|
|
fn impure_else(arg: Option<i32>) {
|
|
|
|
let side_effect = || {
|
|
|
|
println!("return 1");
|
|
|
|
1
|
|
|
|
};
|
|
|
|
let _ = arg.map_or_else(|| side_effect(), |x| x);
|
|
|
|
}
|
|
|
|
|
2020-03-05 12:35:05 -06:00
|
|
|
fn test_map_or_else(arg: Option<u32>) {
|
|
|
|
let _ = arg.map_or_else(|| {
|
|
|
|
let mut y = 1;
|
|
|
|
y = (y + 2 / y) / 2;
|
|
|
|
y = (y + 2 / y) / 2;
|
|
|
|
y
|
|
|
|
}, |x| x * x * x * x);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn negative_tests(arg: Option<u32>) -> u32 {
|
|
|
|
let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
|
|
|
|
for _ in 0..10 {
|
|
|
|
let _ = if let Some(x) = arg {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let _ = if let Some(x) = arg {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
5
|
|
|
|
};
|
|
|
|
7
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let optional = Some(5);
|
|
|
|
let _ = optional.map_or(5, |x| x + 2);
|
|
|
|
let _ = bad1(None);
|
2020-04-25 11:08:23 -05:00
|
|
|
let _ = else_if_option(None);
|
2020-05-09 22:20:57 -05:00
|
|
|
unop_bad(&None, None);
|
2020-03-05 12:35:05 -06:00
|
|
|
let _ = longer_body(None);
|
|
|
|
test_map_or_else(None);
|
|
|
|
let _ = negative_tests(None);
|
2020-08-21 17:43:04 -05:00
|
|
|
let _ = impure_else(None);
|
2020-03-05 12:35:05 -06:00
|
|
|
}
|