@@ -2029,6 +2029,10 @@ pub fn is_must_use_func_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
2029
2029
2030
2030
/// Checks if an expression represents the identity function
2031
2031
/// Only examines closures and `std::convert::identity`
2032
+ ///
2033
+ /// Closure bindings with type annotations and `std::convert::identity` with generic args
2034
+ /// are not considered identity functions because they can guide type inference,
2035
+ /// and removing it may lead to compile errors.
2032
2036
pub fn is_expr_identity_function ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
2033
2037
/// Checks if a function's body represents the identity function. Looks for bodies of the form:
2034
2038
/// * `|x| x`
@@ -2070,8 +2074,18 @@ pub fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool
2070
2074
}
2071
2075
2072
2076
match expr. kind {
2073
- ExprKind :: Closure ( & Closure { body, .. } ) => is_body_identity_function ( cx, cx. tcx . hir ( ) . body ( body) ) ,
2074
- _ => path_def_id ( cx, expr) . map_or ( false , |id| match_def_path ( cx, id, & paths:: CONVERT_IDENTITY ) ) ,
2077
+ ExprKind :: Closure ( & Closure { body, fn_decl, .. } )
2078
+ if fn_decl. inputs . iter ( ) . all ( |ty| matches ! ( ty. kind, TyKind :: Infer ) ) =>
2079
+ {
2080
+ is_body_identity_function ( cx, cx. tcx . hir ( ) . body ( body) )
2081
+ } ,
2082
+ ExprKind :: Path ( QPath :: Resolved ( _, path) )
2083
+ if path. segments . iter ( ) . all ( |seg| seg. infer_args )
2084
+ && let Some ( did) = path. res . opt_def_id ( ) =>
2085
+ {
2086
+ match_def_path ( cx, did, & paths:: CONVERT_IDENTITY )
2087
+ } ,
2088
+ _ => false ,
2075
2089
}
2076
2090
}
2077
2091
0 commit comments