rust/tests/ui/bind_instead_of_map_multipart.fixed

63 lines
1.5 KiB
Rust
Raw Normal View History

//@run-rustfix
#![deny(clippy::bind_instead_of_map)]
#![allow(clippy::blocks_in_if_conditions)]
pub fn main() {
let _ = Some("42").map(|s| if s.len() < 42 { 0 } else { s.len() });
let _ = Some("42").and_then(|s| if s.len() < 42 { None } else { Some(s.len()) });
let _ = Ok::<_, ()>("42").map(|s| if s.len() < 42 { 0 } else { s.len() });
let _ = Ok::<_, ()>("42").and_then(|s| if s.len() < 42 { Err(()) } else { Ok(s.len()) });
let _ = Err::<(), _>("42").map_err(|s| if s.len() < 42 { s.len() + 20 } else { s.len() });
let _ = Err::<(), _>("42").or_else(|s| if s.len() < 42 { Ok(()) } else { Err(s.len()) });
hard_example();
macro_example();
}
fn hard_example() {
Some("42").map(|s| {
if {
if s == "43" {
return 43;
}
s == "42"
} {
return 45;
}
match s.len() {
10 => 2,
20 => {
if foo() {
return {
if foo() {
return 20;
}
println!("foo");
3
};
}
20
},
40 => 30,
_ => 1,
}
});
}
fn foo() -> bool {
true
}
macro_rules! m {
() => {
Some(10)
};
}
fn macro_example() {
let _ = Some("").and_then(|s| if s.len() == 20 { m!() } else { Some(20) });
let _ = Some("").map(|s| if s.len() == 20 { m!() } else { Some(20) });
}