|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 3 | +use clippy_utils::{path_res, peel_blocks}; |
| 4 | +use rustc_hir::def::Res; |
| 5 | +use rustc_hir::def_id::LocalDefId; |
| 6 | +use rustc_hir::intravisit::FnKind; |
| 7 | +use rustc_hir::{Body, ExprKind, FnDecl, FnRetTy}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | +use rustc_span::{Span, sym}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for functions with method calls to `.map(_)` on an arg |
| 15 | + /// of type `Option` as the outermost expression. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// Taking and returning an `Option<T>` may require additional |
| 19 | + /// `Some(_)` and `unwrap` if all you have is a `T`. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```no_run |
| 23 | + /// fn double(param: Option<u32>) -> Option<u32> { |
| 24 | + /// param.map(|x| x * 2) |
| 25 | + /// } |
| 26 | + /// ``` |
| 27 | + /// Use instead: |
| 28 | + /// ```no_run |
| 29 | + /// fn double(param: u32) -> u32 { |
| 30 | + /// param * 2 |
| 31 | + /// } |
| 32 | + /// ``` |
| 33 | + #[clippy::version = "1.86.0"] |
| 34 | + pub SINGLE_OPTION_MAP, |
| 35 | + nursery, |
| 36 | + "Checks for functions with method calls to `.map(_)` on an arg of type `Option` as the outermost expression." |
| 37 | +} |
| 38 | + |
| 39 | +declare_lint_pass!(SingleOptionMap => [SINGLE_OPTION_MAP]); |
| 40 | + |
| 41 | +impl<'tcx> LateLintPass<'tcx> for SingleOptionMap { |
| 42 | + fn check_fn( |
| 43 | + &mut self, |
| 44 | + cx: &LateContext<'tcx>, |
| 45 | + kind: FnKind<'tcx>, |
| 46 | + decl: &'tcx FnDecl<'tcx>, |
| 47 | + body: &'tcx Body<'tcx>, |
| 48 | + span: Span, |
| 49 | + _fn_def: LocalDefId, |
| 50 | + ) { |
| 51 | + if let FnRetTy::Return(_ret) = decl.output |
| 52 | + && matches!(kind, FnKind::ItemFn(_, _, _) | FnKind::Method(_, _)) |
| 53 | + { |
| 54 | + let func_body = peel_blocks(body.value); |
| 55 | + if let ExprKind::MethodCall(method_name, callee, args, _span) = func_body.kind |
| 56 | + && method_name.ident.name == sym::map |
| 57 | + && let callee_type = cx.typeck_results().expr_ty(callee) |
| 58 | + && is_type_diagnostic_item(cx, callee_type, sym::Option) |
| 59 | + && let ExprKind::Path(_path) = callee.kind |
| 60 | + && let Res::Local(_id) = path_res(cx, callee) |
| 61 | + && matches!(path_res(cx, callee), Res::Local(_id)) |
| 62 | + && !matches!(args[0].kind, ExprKind::Path(_)) |
| 63 | + { |
| 64 | + if let ExprKind::Closure(closure) = args[0].kind { |
| 65 | + let Body { params: [..], value } = cx.tcx.hir().body(closure.body); |
| 66 | + if let ExprKind::Call(func, f_args) = value.kind |
| 67 | + && matches!(func.kind, ExprKind::Path(_)) |
| 68 | + && f_args.iter().all(|arg| matches!(arg.kind, ExprKind::Path(_))) |
| 69 | + { |
| 70 | + return; |
| 71 | + } else if let ExprKind::MethodCall(_segment, receiver, method_args, _span) = value.kind |
| 72 | + && matches!(receiver.kind, ExprKind::Path(_)) |
| 73 | + && method_args.iter().all(|arg| matches!(arg.kind, ExprKind::Path(_))) |
| 74 | + && method_args.iter().all(|arg| matches!(path_res(cx, arg), Res::Local(_))) |
| 75 | + { |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + span_lint_and_help( |
| 81 | + cx, |
| 82 | + SINGLE_OPTION_MAP, |
| 83 | + span, |
| 84 | + "`fn` that only maps over argument", |
| 85 | + None, |
| 86 | + "move the `.map` to the caller or to an `_opt` function", |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments