1 use super::{contains_return, BIND_INSTEAD_OF_MAP};
3 in_macro, match_qpath, match_type, method_calls, multispan_sugg_with_applicability, paths, remove_blocks, snippet,
4 snippet_with_macro_callsite, span_lint_and_sugg, span_lint_and_then,
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
9 use rustc_hir::intravisit::{self, Visitor};
10 use rustc_lint::LateContext;
11 use rustc_middle::hir::map::Map;
14 pub(crate) struct OptionAndThenSome;
15 impl BindInsteadOfMap for OptionAndThenSome {
16 const TYPE_NAME: &'static str = "Option";
17 const TYPE_QPATH: &'static [&'static str] = &paths::OPTION;
19 const BAD_METHOD_NAME: &'static str = "and_then";
20 const BAD_VARIANT_NAME: &'static str = "Some";
21 const BAD_VARIANT_QPATH: &'static [&'static str] = &paths::OPTION_SOME;
23 const GOOD_METHOD_NAME: &'static str = "map";
26 pub(crate) struct ResultAndThenOk;
27 impl BindInsteadOfMap for ResultAndThenOk {
28 const TYPE_NAME: &'static str = "Result";
29 const TYPE_QPATH: &'static [&'static str] = &paths::RESULT;
31 const BAD_METHOD_NAME: &'static str = "and_then";
32 const BAD_VARIANT_NAME: &'static str = "Ok";
33 const BAD_VARIANT_QPATH: &'static [&'static str] = &paths::RESULT_OK;
35 const GOOD_METHOD_NAME: &'static str = "map";
38 pub(crate) struct ResultOrElseErrInfo;
39 impl BindInsteadOfMap for ResultOrElseErrInfo {
40 const TYPE_NAME: &'static str = "Result";
41 const TYPE_QPATH: &'static [&'static str] = &paths::RESULT;
43 const BAD_METHOD_NAME: &'static str = "or_else";
44 const BAD_VARIANT_NAME: &'static str = "Err";
45 const BAD_VARIANT_QPATH: &'static [&'static str] = &paths::RESULT_ERR;
47 const GOOD_METHOD_NAME: &'static str = "map_err";
50 pub(crate) trait BindInsteadOfMap {
51 const TYPE_NAME: &'static str;
52 const TYPE_QPATH: &'static [&'static str];
54 const BAD_METHOD_NAME: &'static str;
55 const BAD_VARIANT_NAME: &'static str;
56 const BAD_VARIANT_QPATH: &'static [&'static str];
58 const GOOD_METHOD_NAME: &'static str;
60 fn no_op_msg() -> String {
62 "using `{}.{}({})`, which is a no-op",
64 Self::BAD_METHOD_NAME,
65 Self::BAD_VARIANT_NAME
69 fn lint_msg() -> String {
71 "using `{}.{}(|x| {}(y))`, which is more succinctly expressed as `{}(|x| y)`",
73 Self::BAD_METHOD_NAME,
74 Self::BAD_VARIANT_NAME,
75 Self::GOOD_METHOD_NAME
79 fn lint_closure_autofixable(
82 args: &[hir::Expr<'_>],
83 closure_expr: &hir::Expr<'_>,
84 closure_args_span: Span,
87 if let hir::ExprKind::Call(ref some_expr, ref some_args) = closure_expr.kind;
88 if let hir::ExprKind::Path(ref qpath) = some_expr.kind;
89 if match_qpath(qpath, Self::BAD_VARIANT_QPATH);
90 if some_args.len() == 1;
92 let inner_expr = &some_args[0];
94 if contains_return(inner_expr) {
98 let some_inner_snip = if inner_expr.span.from_expansion() {
99 snippet_with_macro_callsite(cx, inner_expr.span, "_")
101 snippet(cx, inner_expr.span, "_")
104 let closure_args_snip = snippet(cx, closure_args_span, "..");
105 let option_snip = snippet(cx, args[0].span, "..");
106 let note = format!("{}.{}({} {})", option_snip, Self::GOOD_METHOD_NAME, closure_args_snip, some_inner_snip);
111 Self::lint_msg().as_ref(),
114 Applicability::MachineApplicable,
123 fn lint_closure(cx: &LateContext<'_>, expr: &hir::Expr<'_>, closure_expr: &hir::Expr<'_>) {
124 let mut suggs = Vec::new();
125 let can_sugg = find_all_ret_expressions(cx, closure_expr, |ret_expr| {
127 if !in_macro(ret_expr.span);
128 if let hir::ExprKind::Call(ref func_path, ref args) = ret_expr.kind;
129 if let hir::ExprKind::Path(ref qpath) = func_path.kind;
130 if match_qpath(qpath, Self::BAD_VARIANT_QPATH);
132 if !contains_return(&args[0]);
134 suggs.push((ret_expr.span, args[0].span.source_callsite()));
143 span_lint_and_then(cx, BIND_INSTEAD_OF_MAP, expr.span, Self::lint_msg().as_ref(), |diag| {
144 multispan_sugg_with_applicability(
147 Applicability::MachineApplicable,
148 std::iter::once((*method_calls(expr, 1).2.get(0).unwrap(), Self::GOOD_METHOD_NAME.into())).chain(
151 .map(|(span1, span2)| (span1, snippet(cx, span2, "_").into())),
158 /// Lint use of `_.and_then(|x| Some(y))` for `Option`s
159 fn lint(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
160 if !match_type(cx, cx.tables().expr_ty(&args[0]), Self::TYPE_QPATH) {
165 hir::ExprKind::Closure(_, _, body_id, closure_args_span, _) => {
166 let closure_body = cx.tcx.hir().body(body_id);
167 let closure_expr = remove_blocks(&closure_body.value);
169 if !Self::lint_closure_autofixable(cx, expr, args, closure_expr, closure_args_span) {
170 Self::lint_closure(cx, expr, closure_expr);
173 // `_.and_then(Some)` case, which is no-op.
174 hir::ExprKind::Path(ref qpath) if match_qpath(qpath, Self::BAD_VARIANT_QPATH) => {
179 Self::no_op_msg().as_ref(),
180 "use the expression directly",
181 snippet(cx, args[0].span, "..").into(),
182 Applicability::MachineApplicable,
190 /// returns `true` if expr contains match expr desugared from try
191 fn contains_try(expr: &hir::Expr<'_>) -> bool {
196 impl<'hir> intravisit::Visitor<'hir> for TryFinder {
197 type Map = Map<'hir>;
199 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
200 intravisit::NestedVisitorMap::None
203 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
208 hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar) => self.found = true,
209 _ => intravisit::walk_expr(self, expr),
214 let mut visitor = TryFinder { found: false };
215 visitor.visit_expr(expr);
219 fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir hir::Expr<'hir>, callback: F) -> bool
221 F: FnMut(&'hir hir::Expr<'hir>) -> bool,
223 struct RetFinder<F> {
229 struct WithStmtGuarg<'a, F> {
230 val: &'a mut RetFinder<F>,
234 impl<F> RetFinder<F> {
235 fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
236 let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
244 impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
245 type Target = RetFinder<F>;
247 fn deref(&self) -> &Self::Target {
252 impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
253 fn deref_mut(&mut self) -> &mut Self::Target {
258 impl<F> Drop for WithStmtGuarg<'_, F> {
260 self.val.in_stmt = self.prev_in_stmt;
264 impl<'hir, F: FnMut(&'hir hir::Expr<'hir>) -> bool> intravisit::Visitor<'hir> for RetFinder<F> {
265 type Map = Map<'hir>;
267 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
268 intravisit::NestedVisitorMap::None
271 fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'_>) {
272 intravisit::walk_stmt(&mut *self.inside_stmt(true), stmt)
275 fn visit_expr(&mut self, expr: &'hir hir::Expr<'_>) {
281 hir::ExprKind::Ret(Some(expr)) => self.inside_stmt(false).visit_expr(expr),
282 _ => intravisit::walk_expr(self, expr),
286 hir::ExprKind::Match(cond, arms, _) => {
287 self.inside_stmt(true).visit_expr(cond);
289 self.visit_expr(arm.body);
292 hir::ExprKind::Block(..) => intravisit::walk_expr(self, expr),
293 hir::ExprKind::Ret(Some(expr)) => self.visit_expr(expr),
294 _ => self.failed |= !(self.cb)(expr),
300 !contains_try(expr) && {
301 let mut ret_finder = RetFinder {
306 ret_finder.visit_expr(expr);