@@ -8,9 +8,9 @@ use clippy_utils::source::snippet_with_applicability;
88use clippy_utils:: sugg:: Sugg ;
99use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item} ;
1010use clippy_utils:: {
11- eq_expr_value, higher, is_else_clause, is_in_const_context, is_lint_allowed, is_path_lang_item , is_res_lang_ctor ,
12- pat_and_expr_can_be_question_mark , path_res , path_to_local , path_to_local_id , peel_blocks , peel_blocks_with_stmt ,
13- span_contains_cfg, span_contains_comment, sym,
11+ eq_expr_value, fn_def_id_with_node_args , higher, is_else_clause, is_in_const_context, is_lint_allowed,
12+ is_path_lang_item , is_res_lang_ctor , pat_and_expr_can_be_question_mark , path_res , path_to_local , path_to_local_id ,
13+ peel_blocks , peel_blocks_with_stmt , span_contains_cfg, span_contains_comment, sym,
1414} ;
1515use rustc_errors:: Applicability ;
1616use rustc_hir:: LangItem :: { self , OptionNone , OptionSome , ResultErr , ResultOk } ;
@@ -393,8 +393,8 @@ fn check_arm_is_none_or_err<'tcx>(cx: &LateContext<'tcx>, mode: TryMode, arm: &A
393393 && let ExprKind :: Ret ( Some ( wrapped_ret_expr) ) = arm_body. kind
394394 && let ExprKind :: Call ( ok_ctor, [ ret_expr] ) = wrapped_ret_expr. kind
395395 && is_res_lang_ctor ( cx, path_res ( cx, ok_ctor) , ResultErr )
396- // check `...` is `val` from binding
397- && path_to_local_id ( ret_expr, ok_val)
396+ // check if `...` is `val` from binding or `val.into()`
397+ && is_local_or_local_into ( cx , ret_expr, ok_val)
398398 {
399399 true
400400 } else {
@@ -417,6 +417,17 @@ fn check_arm_is_none_or_err<'tcx>(cx: &LateContext<'tcx>, mode: TryMode, arm: &A
417417 }
418418}
419419
420+ /// Check if `expr` is `val` or `val.into()`
421+ fn is_local_or_local_into ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , val : HirId ) -> bool {
422+ let is_into_call = fn_def_id_with_node_args ( cx, expr)
423+ . and_then ( |( fn_def_id, _) | cx. tcx . trait_of_assoc ( fn_def_id) )
424+ . is_some_and ( |trait_def_id| cx. tcx . is_diagnostic_item ( sym:: Into , trait_def_id) ) ;
425+ match expr. kind {
426+ ExprKind :: MethodCall ( _, recv, [ ] , _) | ExprKind :: Call ( _, [ recv] ) => is_into_call && path_to_local_id ( recv, val) ,
427+ _ => path_to_local_id ( expr, val) ,
428+ }
429+ }
430+
420431fn check_arms_are_try < ' tcx > ( cx : & LateContext < ' tcx > , mode : TryMode , arm1 : & Arm < ' tcx > , arm2 : & Arm < ' tcx > ) -> bool {
421432 ( check_arm_is_some_or_ok ( cx, mode, arm1) && check_arm_is_none_or_err ( cx, mode, arm2) )
422433 || ( check_arm_is_some_or_ok ( cx, mode, arm2) && check_arm_is_none_or_err ( cx, mode, arm1) )
0 commit comments