mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 17:07:17 +00:00
8b3ca9a315
* `break` and `continue` statments local to the would-be closure are allowed * don't lint in const contexts * don't lint when yield expressions are used * don't lint when the captures made by the would-be closure conflict with the other branch * don't lint when a field of a local is used when the type could be pontentially moved from * in some cases, don't lint when scrutinee expression conflicts with the captures of the would-be closure
131 lines
2.9 KiB
Rust
131 lines
2.9 KiB
Rust
// run-rustfix
|
|
#![warn(clippy::option_if_let_else)]
|
|
#![allow(clippy::redundant_closure)]
|
|
#![allow(clippy::ref_option_ref)]
|
|
|
|
fn bad1(string: Option<&str>) -> (bool, &str) {
|
|
string.map_or((false, "hello"), |x| (true, x))
|
|
}
|
|
|
|
fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
|
|
if string.is_none() {
|
|
None
|
|
} else if let Some(x) = string {
|
|
Some((true, x))
|
|
} else {
|
|
Some((false, ""))
|
|
}
|
|
}
|
|
|
|
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
|
|
});
|
|
}
|
|
|
|
fn longer_body(arg: Option<u32>) -> u32 {
|
|
arg.map_or(13, |x| {
|
|
let y = x * x;
|
|
y * y
|
|
})
|
|
}
|
|
|
|
fn impure_else(arg: Option<i32>) {
|
|
let side_effect = || {
|
|
println!("return 1");
|
|
1
|
|
};
|
|
let _ = arg.map_or_else(|| side_effect(), |x| x);
|
|
}
|
|
|
|
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);
|
|
let _ = else_if_option(None);
|
|
unop_bad(&None, None);
|
|
let _ = longer_body(None);
|
|
test_map_or_else(None);
|
|
let _ = negative_tests(None);
|
|
let _ = impure_else(None);
|
|
|
|
let _ = Some(0).map_or(0, |x| loop {
|
|
if x == 0 {
|
|
break x;
|
|
}
|
|
});
|
|
|
|
// #7576
|
|
const fn _f(x: Option<u32>) -> u32 {
|
|
// Don't lint, `map_or` isn't const
|
|
if let Some(x) = x { x } else { 10 }
|
|
}
|
|
|
|
// #5822
|
|
let s = String::new();
|
|
// Don't lint, `Some` branch consumes `s`, but else branch uses `s`
|
|
let _ = if let Some(x) = Some(0) {
|
|
let s = s;
|
|
s.len() + x
|
|
} else {
|
|
s.len()
|
|
};
|
|
|
|
let s = String::new();
|
|
// Lint, both branches immutably borrow `s`.
|
|
let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
|
|
|
|
let s = String::new();
|
|
// Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
|
|
let _ = Some(0).map_or(1, |x| {
|
|
let s = s;
|
|
s.len() + x
|
|
});
|
|
|
|
let s = Some(String::new());
|
|
// Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
|
|
let _ = if let Some(x) = &s {
|
|
x.len()
|
|
} else {
|
|
let _s = s;
|
|
10
|
|
};
|
|
}
|