rust/tests/ui/result_map_or_into_option.rs
Nick Torres 91d8a804d3 result_map_or_into_option: add lint to catch manually adpating Result -> Option
Result<T, E> has an `ok()` method that adapts a Result<T,E> into an Option<T>.
It's possible to get around this adapter by writing Result<T,E>.map_or(None, Some).

This lint is implemented as a new variant of the existing
[`option_map_none` lint](https://github.com/rust-lang/rust-clippy/pull/2128)
2020-04-04 03:17:13 -07:00

14 lines
336 B
Rust

// run-rustfix
#![warn(clippy::result_map_or_into_option)]
fn main() {
let opt: Result<u32, &str> = Ok(1);
let _ = opt.map_or(None, Some);
let rewrap = |s: u32| -> Option<u32> { Some(s) };
// A non-Some `f` arg should not emit the lint
let opt: Result<u32, &str> = Ok(1);
let _ = opt.map_or(None, rewrap);
}