Files
rust/tests/ui/bind_instead_of_map.fixed
T
Samuel Tardieu c36706495b Do not propose to refactor when no variant constructor is used
If all the ways to "leave" the closure is through a divergent statement
other than `return` (not detected as a potential return value by the
visitor), do not trigger the lint.
2026-04-16 19:56:25 +02:00

35 lines
1.0 KiB
Rust

#![deny(clippy::bind_instead_of_map)]
#![allow(clippy::manual_filter)]
// need a main anyway, use it get rid of unused warnings too
pub fn main() {
let x = Some(5);
// the easiest cases
let _ = x;
//~^ bind_instead_of_map
let _ = x.map(|o| o + 1);
//~^ bind_instead_of_map
// and an easy counter-example
let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });
// Different type
let x: Result<u32, &str> = Ok(1);
let _ = x;
//~^ bind_instead_of_map
}
pub fn foo() -> Option<String> {
let x = Some(String::from("hello"));
Some("hello".to_owned()).and_then(|s| Some(format!("{}{}", s, x?)))
}
pub fn example2(x: bool) -> Option<&'static str> {
Some("a").and_then(|s| Some(if x { s } else { return None }))
}
fn issue16861(b: bool, res: Result<u32, u32>) {
let _: Result<u32, u32> = res.or_else(|_| panic!("should not happen"));
let _: Result<u32, u32> = res.map_err(|_| if b { panic!("should not happen") } else { 42 });
//~^ bind_instead_of_map
}