Skip to content

Commit 5557596

Browse files
committed
move option_map_or_none to its own module
1 parent bbed852 commit 5557596

File tree

2 files changed

+80
-71
lines changed

2 files changed

+80
-71
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod map_collect_result_unit;
3030
mod map_flatten;
3131
mod ok_expect;
3232
mod option_as_ref_deref;
33+
mod option_map_or_none;
3334
mod option_map_unwrap_or;
3435
mod search_is_some;
3536
mod single_char_insert_string;
@@ -1692,7 +1693,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16921693
unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "unwrap_or");
16931694
}
16941695
},
1695-
["map_or", ..] => lint_map_or_none(cx, expr, arg_lists[0]),
1696+
["map_or", ..] => option_map_or_none::check(cx, expr, arg_lists[0]),
16961697
["and_then", ..] => {
16971698
let biom_option_linted = bind_instead_of_map::OptionAndThenSome::check(cx, expr, arg_lists[0]);
16981699
let biom_result_linted = bind_instead_of_map::ResultAndThenOk::check(cx, expr, arg_lists[0]);
@@ -2431,76 +2432,6 @@ fn lint_map_unwrap_or_else<'tcx>(
24312432
false
24322433
}
24332434

2434-
/// lint use of `_.map_or(None, _)` for `Option`s and `Result`s
2435-
fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map_or_args: &'tcx [hir::Expr<'_>]) {
2436-
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_or_args[0]), sym::option_type);
2437-
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_or_args[0]), sym::result_type);
2438-
2439-
// There are two variants of this `map_or` lint:
2440-
// (1) using `map_or` as an adapter from `Result<T,E>` to `Option<T>`
2441-
// (2) using `map_or` as a combinator instead of `and_then`
2442-
//
2443-
// (For this lint) we don't care if any other type calls `map_or`
2444-
if !is_option && !is_result {
2445-
return;
2446-
}
2447-
2448-
let (lint_name, msg, instead, hint) = {
2449-
let default_arg_is_none = if let hir::ExprKind::Path(ref qpath) = map_or_args[1].kind {
2450-
match_qpath(qpath, &paths::OPTION_NONE)
2451-
} else {
2452-
return;
2453-
};
2454-
2455-
if !default_arg_is_none {
2456-
// nothing to lint!
2457-
return;
2458-
}
2459-
2460-
let f_arg_is_some = if let hir::ExprKind::Path(ref qpath) = map_or_args[2].kind {
2461-
match_qpath(qpath, &paths::OPTION_SOME)
2462-
} else {
2463-
false
2464-
};
2465-
2466-
if is_option {
2467-
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2468-
let func_snippet = snippet(cx, map_or_args[2].span, "..");
2469-
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
2470-
`and_then(..)` instead";
2471-
(
2472-
OPTION_MAP_OR_NONE,
2473-
msg,
2474-
"try using `and_then` instead",
2475-
format!("{0}.and_then({1})", self_snippet, func_snippet),
2476-
)
2477-
} else if f_arg_is_some {
2478-
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
2479-
`ok()` instead";
2480-
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2481-
(
2482-
RESULT_MAP_OR_INTO_OPTION,
2483-
msg,
2484-
"try using `ok` instead",
2485-
format!("{0}.ok()", self_snippet),
2486-
)
2487-
} else {
2488-
// nothing to lint!
2489-
return;
2490-
}
2491-
};
2492-
2493-
span_lint_and_sugg(
2494-
cx,
2495-
lint_name,
2496-
expr.span,
2497-
msg,
2498-
instead,
2499-
hint,
2500-
Applicability::MachineApplicable,
2501-
);
2502-
}
2503-
25042435
/// Used for `lint_binary_expr_with_method_call`.
25052436
#[derive(Copy, Clone)]
25062437
struct BinaryExprInfo<'a> {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::utils::{is_type_diagnostic_item, match_qpath, paths, snippet, span_lint_and_sugg};
2+
use rustc_errors::Applicability;
3+
use rustc_hir as hir;
4+
use rustc_lint::LateContext;
5+
use rustc_span::symbol::sym;
6+
7+
use super::OPTION_MAP_OR_NONE;
8+
use super::RESULT_MAP_OR_INTO_OPTION;
9+
10+
/// lint use of `_.map_or(None, _)` for `Option`s and `Result`s
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map_or_args: &'tcx [hir::Expr<'_>]) {
12+
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_or_args[0]), sym::option_type);
13+
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_or_args[0]), sym::result_type);
14+
15+
// There are two variants of this `map_or` lint:
16+
// (1) using `map_or` as an adapter from `Result<T,E>` to `Option<T>`
17+
// (2) using `map_or` as a combinator instead of `and_then`
18+
//
19+
// (For this lint) we don't care if any other type calls `map_or`
20+
if !is_option && !is_result {
21+
return;
22+
}
23+
24+
let (lint_name, msg, instead, hint) = {
25+
let default_arg_is_none = if let hir::ExprKind::Path(ref qpath) = map_or_args[1].kind {
26+
match_qpath(qpath, &paths::OPTION_NONE)
27+
} else {
28+
return;
29+
};
30+
31+
if !default_arg_is_none {
32+
// nothing to lint!
33+
return;
34+
}
35+
36+
let f_arg_is_some = if let hir::ExprKind::Path(ref qpath) = map_or_args[2].kind {
37+
match_qpath(qpath, &paths::OPTION_SOME)
38+
} else {
39+
false
40+
};
41+
42+
if is_option {
43+
let self_snippet = snippet(cx, map_or_args[0].span, "..");
44+
let func_snippet = snippet(cx, map_or_args[2].span, "..");
45+
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
46+
`and_then(..)` instead";
47+
(
48+
OPTION_MAP_OR_NONE,
49+
msg,
50+
"try using `and_then` instead",
51+
format!("{0}.and_then({1})", self_snippet, func_snippet),
52+
)
53+
} else if f_arg_is_some {
54+
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
55+
`ok()` instead";
56+
let self_snippet = snippet(cx, map_or_args[0].span, "..");
57+
(
58+
RESULT_MAP_OR_INTO_OPTION,
59+
msg,
60+
"try using `ok` instead",
61+
format!("{0}.ok()", self_snippet),
62+
)
63+
} else {
64+
// nothing to lint!
65+
return;
66+
}
67+
};
68+
69+
span_lint_and_sugg(
70+
cx,
71+
lint_name,
72+
expr.span,
73+
msg,
74+
instead,
75+
hint,
76+
Applicability::MachineApplicable,
77+
);
78+
}

0 commit comments

Comments
 (0)