|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 | 2 | use clippy_utils::ty::is_type_diagnostic_item;
|
3 |
| -use clippy_utils::{is_adjusted, is_qpath_def_path, is_trait_method, match_var, paths, remove_blocks}; |
| 3 | +use clippy_utils::{is_expr_identity_function, is_trait_method}; |
4 | 4 | use if_chain::if_chain;
|
5 | 5 | use rustc_errors::Applicability;
|
6 |
| -use rustc_hir::{Body, Expr, ExprKind, Pat, PatKind, QPath, StmtKind}; |
| 6 | +use rustc_hir::{Expr, ExprKind}; |
7 | 7 | use rustc_lint::{LateContext, LateLintPass};
|
8 | 8 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
9 | 9 | use rustc_span::sym;
|
@@ -74,53 +74,3 @@ fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a
|
74 | 74 | }
|
75 | 75 | }
|
76 | 76 | }
|
77 |
| - |
78 |
| -/// Checks if an expression represents the identity function |
79 |
| -/// Only examines closures and `std::convert::identity` |
80 |
| -fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
81 |
| - match expr.kind { |
82 |
| - ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)), |
83 |
| - ExprKind::Path(ref path) => is_qpath_def_path(cx, path, expr.hir_id, &paths::CONVERT_IDENTITY), |
84 |
| - _ => false, |
85 |
| - } |
86 |
| -} |
87 |
| - |
88 |
| -/// Checks if a function's body represents the identity function |
89 |
| -/// Looks for bodies of the form `|x| x`, `|x| return x`, `|x| { return x }` or `|x| { |
90 |
| -/// return x; }` |
91 |
| -fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool { |
92 |
| - let params = func.params; |
93 |
| - let body = remove_blocks(&func.value); |
94 |
| - |
95 |
| - // if there's less/more than one parameter, then it is not the identity function |
96 |
| - if params.len() != 1 { |
97 |
| - return false; |
98 |
| - } |
99 |
| - |
100 |
| - match body.kind { |
101 |
| - ExprKind::Path(QPath::Resolved(None, _)) => match_expr_param(cx, body, params[0].pat), |
102 |
| - ExprKind::Ret(Some(ret_val)) => match_expr_param(cx, ret_val, params[0].pat), |
103 |
| - ExprKind::Block(block, _) => { |
104 |
| - if_chain! { |
105 |
| - if block.stmts.len() == 1; |
106 |
| - if let StmtKind::Semi(expr) | StmtKind::Expr(expr) = block.stmts[0].kind; |
107 |
| - if let ExprKind::Ret(Some(ret_val)) = expr.kind; |
108 |
| - then { |
109 |
| - match_expr_param(cx, ret_val, params[0].pat) |
110 |
| - } else { |
111 |
| - false |
112 |
| - } |
113 |
| - } |
114 |
| - }, |
115 |
| - _ => false, |
116 |
| - } |
117 |
| -} |
118 |
| - |
119 |
| -/// Returns true iff an expression returns the same thing as a parameter's pattern |
120 |
| -fn match_expr_param(cx: &LateContext<'_>, expr: &Expr<'_>, pat: &Pat<'_>) -> bool { |
121 |
| - if let PatKind::Binding(_, _, ident, _) = pat.kind { |
122 |
| - match_var(expr, ident.name) && !(cx.typeck_results().hir_owner == expr.hir_id.owner && is_adjusted(cx, expr)) |
123 |
| - } else { |
124 |
| - false |
125 |
| - } |
126 |
| -} |
0 commit comments