@@ -4,8 +4,8 @@ use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
4
4
use clippy_utils:: sugg:: Sugg ;
5
5
use clippy_utils:: ty:: { is_copy, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable, type_is_unsafe_function} ;
6
6
use clippy_utils:: {
7
- CaptureKind , can_move_expr_to_closure, is_else_clause, is_lint_allowed, is_res_lang_ctor, path_res ,
8
- path_to_local_id, peel_blocks, peel_hir_expr_refs, peel_hir_expr_while,
7
+ CaptureKind , can_move_expr_to_closure, expr_requires_coercion , is_else_clause, is_lint_allowed, is_res_lang_ctor,
8
+ path_res , path_to_local_id, peel_blocks, peel_hir_expr_refs, peel_hir_expr_while,
9
9
} ;
10
10
use rustc_ast:: util:: parser:: ExprPrecedence ;
11
11
use rustc_errors:: Applicability ;
73
73
}
74
74
75
75
// `map` won't perform any adjustments.
76
- if expr_has_type_coercion ( cx, expr) {
76
+ if expr_requires_coercion ( cx, expr) {
77
77
return None ;
78
78
}
79
79
@@ -272,71 +272,3 @@ pub(super) fn try_parse_pattern<'tcx>(
272
272
fn is_none_expr ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
273
273
is_res_lang_ctor ( cx, path_res ( cx, peel_blocks ( expr) ) , OptionNone )
274
274
}
275
-
276
- fn expr_ty_adjusted ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
277
- cx. typeck_results ( )
278
- . expr_adjustments ( expr)
279
- . iter ( )
280
- // We do not care about exprs with `NeverToAny` adjustments, such as `panic!` call.
281
- . any ( |adj| !matches ! ( adj. kind, Adjust :: NeverToAny ) )
282
- }
283
-
284
- fn expr_has_type_coercion < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' tcx > ) -> bool {
285
- if expr. span . from_expansion ( ) {
286
- return false ;
287
- }
288
- if expr_ty_adjusted ( cx, expr) {
289
- return true ;
290
- }
291
-
292
- // Identify coercion sites and recursively check it those sites
293
- // actually has type adjustments.
294
- match expr. kind {
295
- // Function/method calls, including enum initialization.
296
- ExprKind :: Call ( _, args) | ExprKind :: MethodCall ( _, _, args, _) if let Some ( def_id) = fn_def_id ( cx, expr) => {
297
- let fn_sig = cx. tcx . fn_sig ( def_id) . instantiate_identity ( ) ;
298
- if !fn_sig. output ( ) . skip_binder ( ) . has_type_flags ( TypeFlags :: HAS_TY_PARAM ) {
299
- return false ;
300
- }
301
- let mut args_with_ty_param = fn_sig
302
- . inputs ( )
303
- . skip_binder ( )
304
- . iter ( )
305
- . zip ( args)
306
- . filter_map ( |( arg_ty, arg) | if arg_ty. has_type_flags ( TypeFlags :: HAS_TY_PARAM ) {
307
- Some ( arg)
308
- } else {
309
- None
310
- } ) ;
311
- args_with_ty_param. any ( |arg| expr_has_type_coercion ( cx, arg) )
312
- } ,
313
- // Struct/union initialization.
314
- ExprKind :: Struct ( _, fields, _) => {
315
- fields. iter ( ) . map ( |expr_field| expr_field. expr ) . any ( |ex| expr_has_type_coercion ( cx, ex) )
316
- } ,
317
- // those two `ref` keywords cannot be removed
318
- #[ allow( clippy:: needless_borrow) ]
319
- // Function results, including the final line of a block or a `return` expression.
320
- ExprKind :: Block ( hir:: Block { expr : Some ( ref ret_expr) , .. } , _) |
321
- ExprKind :: Ret ( Some ( ref ret_expr) ) => expr_has_type_coercion ( cx, ret_expr) ,
322
-
323
- // ===== Coercion-propagation expressions =====
324
-
325
- // Array, where the type is `[U; n]`.
326
- ExprKind :: Array ( elems) |
327
- // Tuple, `(U_0, U_1, ..., U_n)`.
328
- ExprKind :: Tup ( elems) => {
329
- elems. iter ( ) . any ( |elem| expr_has_type_coercion ( cx, elem) )
330
- } ,
331
- // Array but with repeating syntax.
332
- ExprKind :: Repeat ( rep_elem, _) => expr_has_type_coercion ( cx, rep_elem) ,
333
- // Others that may contain coercion sites.
334
- ExprKind :: If ( _, then, maybe_else) => {
335
- expr_has_type_coercion ( cx, then) || maybe_else. is_some_and ( |e| expr_has_type_coercion ( cx, e) )
336
- }
337
- ExprKind :: Match ( _, arms, _) => {
338
- arms. iter ( ) . map ( |arm| arm. body ) . any ( |body| expr_has_type_coercion ( cx, body) )
339
- }
340
- _ => false
341
- }
342
- }
0 commit comments