1 use crate::utils::{is_type_diagnostic_item, match_qpath, snippet, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
5 use rustc_lint::LateContext;
7 use super::UNNECESSARY_LAZY_EVALUATIONS;
9 // Return true if the expression is an accessor of any of the arguments
10 fn expr_uses_argument(expr: &hir::Expr<'_>, params: &[hir::Param<'_>]) -> bool {
11 params.iter().any(|arg| {
13 if let hir::PatKind::Binding(_, _, ident, _) = arg.pat.kind;
14 if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.kind;
15 if let [p, ..] = path.segments;
17 ident.name == p.ident.name
25 fn match_any_qpath(path: &hir::QPath<'_>, paths: &[&[&str]]) -> bool {
26 paths.iter().any(|candidate| match_qpath(path, candidate))
29 fn can_simplify(expr: &hir::Expr<'_>, params: &[hir::Param<'_>], variant_calls: bool) -> bool {
31 // Closures returning literals can be unconditionally simplified
32 hir::ExprKind::Lit(_) => true,
34 hir::ExprKind::Index(ref object, ref index) => {
35 // arguments are not being indexed into
36 if expr_uses_argument(object, params) {
39 // arguments are not used as index
40 !expr_uses_argument(index, params)
44 // Reading fields can be simplified if the object is not an argument of the closure
45 hir::ExprKind::Field(ref object, _) => !expr_uses_argument(object, params),
47 // Paths can be simplified if the root is not the argument, this also covers None
48 hir::ExprKind::Path(_) => !expr_uses_argument(expr, params),
50 // Calls to Some, Ok, Err can be considered literals if they don't derive an argument
51 hir::ExprKind::Call(ref func, ref args) => if_chain! {
52 if variant_calls; // Disable lint when rules conflict with bind_instead_of_map
53 if let hir::ExprKind::Path(ref path) = func.kind;
54 if match_any_qpath(path, &[&["Some"], &["Ok"], &["Err"]]);
56 // Recursively check all arguments
57 args.iter().all(|arg| can_simplify(arg, params, variant_calls))
63 // For anything more complex than the above, a closure is probably the right solution,
64 // or the case is handled by an other lint
69 /// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
70 /// replaced with `<fn>(return value of simple closure)`
71 pub(super) fn lint<'tcx>(
72 cx: &LateContext<'tcx>,
73 expr: &'tcx hir::Expr<'_>,
74 args: &'tcx [hir::Expr<'_>],
75 allow_variant_calls: bool,
78 let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]), sym!(option_type));
79 let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]), sym!(result_type));
81 if is_option || is_result {
82 if let hir::ExprKind::Closure(_, _, eid, _, _) = args[1].kind {
83 let body = cx.tcx.hir().body(eid);
85 let params = &body.params;
87 if can_simplify(ex, params, allow_variant_calls) {
88 let msg = if is_option {
89 "unnecessary closure used to substitute value for `Option::None`"
91 "unnecessary closure used to substitute value for `Result::Err`"
96 UNNECESSARY_LAZY_EVALUATIONS,
99 &format!("Use `{}` instead", simplify_using),
102 snippet(cx, args[0].span, ".."),
104 snippet(cx, ex.span, ".."),
106 Applicability::MachineApplicable,