Skip to content

Commit 91d8a80

Browse files
committed
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](#2128)
1 parent 7907abe commit 91d8a80

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,
@@ -1273,6 +1274,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12731274
LintId::of(&methods::OPTION_AS_REF_DEREF),
12741275
LintId::of(&methods::OPTION_MAP_OR_NONE),
12751276
LintId::of(&methods::OR_FUN_CALL),
1277+
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
12761278
LintId::of(&methods::SEARCH_IS_SOME),
12771279
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
12781280
LintId::of(&methods::SINGLE_CHAR_PATTERN),
@@ -1453,6 +1455,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14531455
LintId::of(&methods::NEW_RET_NO_SELF),
14541456
LintId::of(&methods::OK_EXPECT),
14551457
LintId::of(&methods::OPTION_MAP_OR_NONE),
1458+
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
14561459
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
14571460
LintId::of(&methods::STRING_EXTEND_CHARS),
14581461
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
@@ -330,6 +330,24 @@ declare_clippy_lint! {
330330
"using `Option.map_or(None, f)`, which is more succinctly expressed as `and_then(f)`"
331331
}
332332

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

2520-
/// lint use of `_.map_or(None, _)` for `Option`s
2539+
/// lint use of `_.map_or(None, _)` for `Option`s and `Result`s
25212540
fn lint_map_or_none<'a, 'tcx>(
25222541
cx: &LateContext<'a, 'tcx>,
25232542
expr: &'tcx hir::Expr<'_>,
25242543
map_or_args: &'tcx [hir::Expr<'_>],
25252544
) {
2526-
if match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION) {
2527-
// check if the first non-self argument to map_or() is None
2528-
let map_or_arg_is_none = if let hir::ExprKind::Path(ref qpath) = map_or_args[1].kind {
2529-
match_qpath(qpath, &paths::OPTION_NONE)
2530-
} else {
2531-
false
2532-
};
2545+
let is_option = match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION);
2546+
let is_result = match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::RESULT);
2547+
2548+
// There are two variants of this `map_or` lint:
2549+
// (1) using `map_or` as an adapter from `Result<T,E>` to `Option<T>`
2550+
// (2) using `map_or` as a combinator instead of `and_then`
2551+
//
2552+
// (For this lint) we don't care if any other type calls `map_or`
2553+
if !is_option && !is_result {
2554+
return;
2555+
}
25332556

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

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)