Skip to content

Commit 2fd92d7

Browse files
nickrtorresrokob
authored andcommitted
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](rust-lang#2128)
1 parent 0d467a7 commit 2fd92d7

File tree

7 files changed

+131
-25
lines changed

7 files changed

+131
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,7 @@ Released 2018-09-13
14481448
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
14491449
[`rest_pat_in_fully_bound_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#rest_pat_in_fully_bound_structs
14501450
[`result_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_expect_used
1451+
[`result_map_or_into_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_or_into_option
14511452
[`result_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unit_fn
14521453
[`result_map_unwrap_or_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unwrap_or_else
14531454
[`result_unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_unwrap_used

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
666666
&methods::OPTION_UNWRAP_USED,
667667
&methods::OR_FUN_CALL,
668668
&methods::RESULT_EXPECT_USED,
669+
&methods::RESULT_MAP_OR_INTO_OPTION,
669670
&methods::RESULT_MAP_UNWRAP_OR_ELSE,
670671
&methods::RESULT_UNWRAP_USED,
671672
&methods::SEARCH_IS_SOME,
@@ -1281,6 +1282,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12811282
LintId::of(&methods::OPTION_AS_REF_DEREF),
12821283
LintId::of(&methods::OPTION_MAP_OR_NONE),
12831284
LintId::of(&methods::OR_FUN_CALL),
1285+
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
12841286
LintId::of(&methods::SEARCH_IS_SOME),
12851287
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
12861288
LintId::of(&methods::SINGLE_CHAR_PATTERN),
@@ -1459,6 +1461,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14591461
LintId::of(&methods::NEW_RET_NO_SELF),
14601462
LintId::of(&methods::OK_EXPECT),
14611463
LintId::of(&methods::OPTION_MAP_OR_NONE),
1464+
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
14621465
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
14631466
LintId::of(&methods::STRING_EXTEND_CHARS),
14641467
LintId::of(&methods::UNNECESSARY_FOLD),

clippy_lints/src/methods/mod.rs

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ declare_clippy_lint! {
331331
"using `Option.map_or(None, f)`, which is more succinctly expressed as `and_then(f)`"
332332
}
333333

334+
declare_clippy_lint! {
335+
/// **What it does:** Checks for usage of `_.map_or(None, Some)`.
336+
///
337+
/// **Why is this bad?** Readability, this can be written more concisely as
338+
/// `_.ok()`.
339+
///
340+
/// **Known problems:**
341+
///
342+
/// **Example:**
343+
/// ```rust
344+
/// # let opt = Some(1);
345+
/// # let r = opt.map_or(None, Some);
346+
/// ```
347+
pub RESULT_MAP_OR_INTO_OPTION,
348+
style,
349+
"using `Result.map_or(None, Some)`, which is more succinctly expressed as `ok()`"
350+
}
351+
334352
declare_clippy_lint! {
335353
/// **What it does:** Checks for usage of `_.and_then(|x| Some(y))`.
336354
///
@@ -1249,6 +1267,7 @@ declare_lint_pass!(Methods => [
12491267
OPTION_MAP_UNWRAP_OR,
12501268
OPTION_MAP_UNWRAP_OR_ELSE,
12511269
RESULT_MAP_UNWRAP_OR_ELSE,
1270+
RESULT_MAP_OR_INTO_OPTION,
12521271
OPTION_MAP_OR_NONE,
12531272
OPTION_AND_THEN_SOME,
12541273
OR_FUN_CALL,
@@ -2524,37 +2543,73 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
25242543
}
25252544
}
25262545

2527-
/// lint use of `_.map_or(None, _)` for `Option`s
2546+
/// lint use of `_.map_or(None, _)` for `Option`s and `Result`s
25282547
fn lint_map_or_none<'a, 'tcx>(
25292548
cx: &LateContext<'a, 'tcx>,
25302549
expr: &'tcx hir::Expr<'_>,
25312550
map_or_args: &'tcx [hir::Expr<'_>],
25322551
) {
2533-
if match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION) {
2534-
// check if the first non-self argument to map_or() is None
2535-
let map_or_arg_is_none = if let hir::ExprKind::Path(ref qpath) = map_or_args[1].kind {
2536-
match_qpath(qpath, &paths::OPTION_NONE)
2537-
} else {
2538-
false
2539-
};
2552+
let is_option = match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION);
2553+
let is_result = match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::RESULT);
2554+
2555+
// There are two variants of this `map_or` lint:
2556+
// (1) using `map_or` as an adapter from `Result<T,E>` to `Option<T>`
2557+
// (2) using `map_or` as a combinator instead of `and_then`
2558+
//
2559+
// (For this lint) we don't care if any other type calls `map_or`
2560+
if !is_option && !is_result {
2561+
return;
2562+
}
25402563

2541-
if map_or_arg_is_none {
2542-
// lint message
2543-
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
2544-
`and_then(f)` instead";
2545-
let map_or_self_snippet = snippet(cx, map_or_args[0].span, "..");
2546-
let map_or_func_snippet = snippet(cx, map_or_args[2].span, "..");
2547-
let hint = format!("{0}.and_then({1})", map_or_self_snippet, map_or_func_snippet);
2548-
span_lint_and_sugg(
2549-
cx,
2550-
OPTION_MAP_OR_NONE,
2551-
expr.span,
2552-
msg,
2553-
"try using `and_then` instead",
2554-
hint,
2555-
Applicability::MachineApplicable,
2556-
);
2557-
}
2564+
let default_arg_is_none = if let hir::ExprKind::Path(ref qpath) = map_or_args[1].kind {
2565+
match_qpath(qpath, &paths::OPTION_NONE)
2566+
} else {
2567+
false
2568+
};
2569+
2570+
// This is really only needed if `is_result` holds. Computing it here
2571+
// makes `mess`'s assignment a bit easier, so just compute it here.
2572+
let f_arg_is_some = if let hir::ExprKind::Path(ref qpath) = map_or_args[2].kind {
2573+
match_qpath(qpath, &paths::OPTION_SOME)
2574+
} else {
2575+
false
2576+
};
2577+
2578+
let mess = if is_option && default_arg_is_none {
2579+
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2580+
let func_snippet = snippet(cx, map_or_args[2].span, "..");
2581+
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
2582+
`and_then(f)` instead";
2583+
Some((
2584+
OPTION_MAP_OR_NONE,
2585+
msg,
2586+
"try using `and_then` instead",
2587+
format!("{0}.and_then({1})", self_snippet, func_snippet),
2588+
))
2589+
} else if is_result && f_arg_is_some {
2590+
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
2591+
`ok()` instead";
2592+
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2593+
Some((
2594+
RESULT_MAP_OR_INTO_OPTION,
2595+
msg,
2596+
"try using `ok` instead",
2597+
format!("{0}.ok()", self_snippet),
2598+
))
2599+
} else {
2600+
None
2601+
};
2602+
2603+
if let Some((lint, msg, instead, hint)) = mess {
2604+
span_lint_and_sugg(
2605+
cx,
2606+
lint,
2607+
expr.span,
2608+
msg,
2609+
instead,
2610+
hint,
2611+
Applicability::MachineApplicable,
2612+
);
25582613
}
25592614
}
25602615

src/lintlist/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
18231823
deprecation: None,
18241824
module: "methods",
18251825
},
1826+
Lint {
1827+
name: "result_map_or_into_option",
1828+
group: "style",
1829+
desc: "using `Result.map_or(None, Some)`, which is more succinctly expressed as `ok()`",
1830+
deprecation: None,
1831+
module: "methods",
1832+
},
18261833
Lint {
18271834
name: "result_map_unit_fn",
18281835
group: "complexity",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::result_map_or_into_option)]
4+
5+
fn main() {
6+
let opt: Result<u32, &str> = Ok(1);
7+
let _ = opt.ok();
8+
9+
let rewrap = |s: u32| -> Option<u32> {
10+
Some(s)
11+
};
12+
13+
// A non-Some `f` arg should not emit the lint
14+
let opt: Result<u32, &str> = Ok(1);
15+
let _ = opt.map_or(None, rewrap);
16+
}

tests/ui/result_map_or_into_option.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::result_map_or_into_option)]
4+
5+
fn main() {
6+
let opt: Result<u32, &str> = Ok(1);
7+
let _ = opt.map_or(None, Some);
8+
9+
let rewrap = |s: u32| -> Option<u32> { Some(s) };
10+
11+
// A non-Some `f` arg should not emit the lint
12+
let opt: Result<u32, &str> = Ok(1);
13+
let _ = opt.map_or(None, rewrap);
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling `ok()` instead
2+
--> $DIR/result_map_or_into_option.rs:7:13
3+
|
4+
LL | let _ = opt.map_or(None, Some);
5+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try using `ok` instead: `opt.ok()`
6+
|
7+
= note: `-D clippy::result-map-or-into-option` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)