|
1 |
| -use crate::utils::{is_adjusted, iter_input_pats, snippet_opt, span_lint_and_then}; |
| 1 | +use crate::utils::{is_adjusted, iter_input_pats, snippet_opt, span_lint_and_then, type_is_unsafe_function}; |
| 2 | +use if_chain::if_chain; |
2 | 3 | use rustc::hir::*;
|
3 | 4 | use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
4 | 5 | use rustc::ty;
|
@@ -59,56 +60,136 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
|
59 | 60 | if let ExprKind::Closure(_, ref decl, eid, _, _) = expr.node {
|
60 | 61 | let body = cx.tcx.hir().body(eid);
|
61 | 62 | let ex = &body.value;
|
62 |
| - if let ExprKind::Call(ref caller, ref args) = ex.node { |
63 |
| - if args.len() != decl.inputs.len() { |
64 |
| - // Not the same number of arguments, there |
65 |
| - // is no way the closure is the same as the function |
66 |
| - return; |
67 |
| - } |
68 |
| - if is_adjusted(cx, ex) || args.iter().any(|arg| is_adjusted(cx, arg)) { |
69 |
| - // Are the expression or the arguments type-adjusted? Then we need the closure |
70 |
| - return; |
71 |
| - } |
| 63 | + |
| 64 | + if_chain!( |
| 65 | + if let ExprKind::Call(ref caller, ref args) = ex.node; |
| 66 | + |
| 67 | + // Not the same number of arguments, there is no way the closure is the same as the function return; |
| 68 | + if args.len() == decl.inputs.len(); |
| 69 | + |
| 70 | + // Are the expression or the arguments type-adjusted? Then we need the closure |
| 71 | + if !(is_adjusted(cx, ex) || args.iter().any(|arg| is_adjusted(cx, arg))); |
| 72 | + |
72 | 73 | let fn_ty = cx.tables.expr_ty(caller);
|
73 |
| - match fn_ty.sty { |
74 |
| - // Is it an unsafe function? They don't implement the closure traits |
75 |
| - ty::FnDef(..) | ty::FnPtr(_) => { |
76 |
| - let sig = fn_ty.fn_sig(cx.tcx); |
77 |
| - if sig.skip_binder().unsafety == Unsafety::Unsafe || sig.skip_binder().output().sty == ty::Never { |
78 |
| - return; |
| 74 | + if !type_is_unsafe_function(cx, fn_ty); |
| 75 | + |
| 76 | + if compare_inputs(&mut iter_input_pats(decl, body), &mut args.into_iter()); |
| 77 | + |
| 78 | + then { |
| 79 | + span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| { |
| 80 | + if let Some(snippet) = snippet_opt(cx, caller.span) { |
| 81 | + db.span_suggestion( |
| 82 | + expr.span, |
| 83 | + "remove closure as shown", |
| 84 | + snippet, |
| 85 | + Applicability::MachineApplicable, |
| 86 | + ); |
79 | 87 | }
|
80 |
| - }, |
81 |
| - _ => (), |
| 88 | + }); |
82 | 89 | }
|
83 |
| - for (a1, a2) in iter_input_pats(decl, body).zip(args) { |
84 |
| - if let PatKind::Binding(.., ident, _) = a1.pat.node { |
85 |
| - // XXXManishearth Should I be checking the binding mode here? |
86 |
| - if let ExprKind::Path(QPath::Resolved(None, ref p)) = a2.node { |
87 |
| - if p.segments.len() != 1 { |
88 |
| - // If it's a proper path, it can't be a local variable |
89 |
| - return; |
90 |
| - } |
91 |
| - if p.segments[0].ident.name != ident.name { |
92 |
| - // The two idents should be the same |
93 |
| - return; |
94 |
| - } |
95 |
| - } else { |
96 |
| - return; |
97 |
| - } |
98 |
| - } else { |
99 |
| - return; |
100 |
| - } |
101 |
| - } |
102 |
| - span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| { |
103 |
| - if let Some(snippet) = snippet_opt(cx, caller.span) { |
| 90 | + ); |
| 91 | + |
| 92 | + if_chain!( |
| 93 | + if let ExprKind::MethodCall(ref path, _, ref args) = ex.node; |
| 94 | + |
| 95 | + // Not the same number of arguments, there is no way the closure is the same as the function return; |
| 96 | + if args.len() == decl.inputs.len(); |
| 97 | + |
| 98 | + // Are the expression or the arguments type-adjusted? Then we need the closure |
| 99 | + if !(is_adjusted(cx, ex) || args.iter().skip(1).any(|arg| is_adjusted(cx, arg))); |
| 100 | + |
| 101 | + let method_def_id = cx.tables.type_dependent_defs()[ex.hir_id].def_id(); |
| 102 | + if !type_is_unsafe_function(cx, cx.tcx.type_of(method_def_id)); |
| 103 | + |
| 104 | + if compare_inputs(&mut iter_input_pats(decl, body), &mut args.into_iter()); |
| 105 | + |
| 106 | + if let Some(name) = get_ufcs_type_name(cx, method_def_id, &args[0]); |
| 107 | + |
| 108 | + then { |
| 109 | + span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| { |
104 | 110 | db.span_suggestion(
|
105 | 111 | expr.span,
|
106 | 112 | "remove closure as shown",
|
107 |
| - snippet, |
| 113 | + format!("{}::{}", name, path.ident.name), |
108 | 114 | Applicability::MachineApplicable,
|
109 | 115 | );
|
| 116 | + }); |
| 117 | + } |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// Tries to determine the type for universal function call to be used instead of the closure |
| 123 | +fn get_ufcs_type_name( |
| 124 | + cx: &LateContext<'_, '_>, |
| 125 | + method_def_id: def_id::DefId, |
| 126 | + self_arg: &Expr, |
| 127 | +) -> std::option::Option<String> { |
| 128 | + let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0].sty; |
| 129 | + let actual_type_of_self = &cx.tables.node_id_to_type(self_arg.hir_id).sty; |
| 130 | + |
| 131 | + if let Some(trait_id) = cx.tcx.trait_of_item(method_def_id) { |
| 132 | + //if the method expectes &self, ufcs requires explicit borrowing so closure can't be removed |
| 133 | + return match (expected_type_of_self, actual_type_of_self) { |
| 134 | + (ty::Ref(_, _, _), ty::Ref(_, _, _)) => Some(cx.tcx.item_path_str(trait_id)), |
| 135 | + (l, r) => match (l, r) { |
| 136 | + (ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _)) => None, |
| 137 | + (_, _) => Some(cx.tcx.item_path_str(trait_id)), |
| 138 | + }, |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + cx.tcx.impl_of_method(method_def_id).and_then(|_| { |
| 143 | + //a type may implicitly implement other types methods (e.g. Deref) |
| 144 | + if match_types(expected_type_of_self, actual_type_of_self) { |
| 145 | + return Some(get_type_name(cx, &actual_type_of_self)); |
| 146 | + } |
| 147 | + None |
| 148 | + }) |
| 149 | +} |
| 150 | + |
| 151 | +fn match_types(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool { |
| 152 | + match (lhs, rhs) { |
| 153 | + (ty::Bool, ty::Bool) |
| 154 | + | (ty::Char, ty::Char) |
| 155 | + | (ty::Int(_), ty::Int(_)) |
| 156 | + | (ty::Uint(_), ty::Uint(_)) |
| 157 | + | (ty::Str, ty::Str) => true, |
| 158 | + (ty::Ref(_, t1, _), ty::Ref(_, t2, _)) |
| 159 | + | (ty::Array(t1, _), ty::Array(t2, _)) |
| 160 | + | (ty::Slice(t1), ty::Slice(t2)) => match_types(&t1.sty, &t2.sty), |
| 161 | + (ty::Adt(def1, _), ty::Adt(def2, _)) => def1 == def2, |
| 162 | + (_, _) => false, |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +fn get_type_name(cx: &LateContext<'_, '_>, kind: &ty::TyKind<'_>) -> String { |
| 167 | + match kind { |
| 168 | + ty::Adt(t, _) => cx.tcx.item_path_str(t.did), |
| 169 | + ty::Ref(_, r, _) => get_type_name(cx, &r.sty), |
| 170 | + _ => kind.to_string(), |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +fn compare_inputs(closure_inputs: &mut dyn Iterator<Item = &Arg>, call_args: &mut dyn Iterator<Item = &Expr>) -> bool { |
| 175 | + for (closure_input, function_arg) in closure_inputs.zip(call_args) { |
| 176 | + if let PatKind::Binding(_, _, _, ident, _) = closure_input.pat.node { |
| 177 | + // XXXManishearth Should I be checking the binding mode here? |
| 178 | + if let ExprKind::Path(QPath::Resolved(None, ref p)) = function_arg.node { |
| 179 | + if p.segments.len() != 1 { |
| 180 | + // If it's a proper path, it can't be a local variable |
| 181 | + return false; |
110 | 182 | }
|
111 |
| - }); |
| 183 | + if p.segments[0].ident.name != ident.name { |
| 184 | + // The two idents should be the same |
| 185 | + return false; |
| 186 | + } |
| 187 | + } else { |
| 188 | + return false; |
| 189 | + } |
| 190 | + } else { |
| 191 | + return false; |
112 | 192 | }
|
113 | 193 | }
|
| 194 | + true |
114 | 195 | }
|
0 commit comments