@@ -8,9 +8,9 @@ use clippy_utils::source::snippet_with_applicability;
8
8
use clippy_utils:: sugg:: Sugg ;
9
9
use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item} ;
10
10
use 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,
14
14
} ;
15
15
use rustc_errors:: Applicability ;
16
16
use 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
393
393
&& let ExprKind :: Ret ( Some ( wrapped_ret_expr) ) = arm_body. kind
394
394
&& let ExprKind :: Call ( ok_ctor, [ ret_expr] ) = wrapped_ret_expr. kind
395
395
&& 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)
398
398
{
399
399
true
400
400
} else {
@@ -417,6 +417,17 @@ fn check_arm_is_none_or_err<'tcx>(cx: &LateContext<'tcx>, mode: TryMode, arm: &A
417
417
}
418
418
}
419
419
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
+
420
431
fn check_arms_are_try < ' tcx > ( cx : & LateContext < ' tcx > , mode : TryMode , arm1 : & Arm < ' tcx > , arm2 : & Arm < ' tcx > ) -> bool {
421
432
( check_arm_is_some_or_ok ( cx, mode, arm1) && check_arm_is_none_or_err ( cx, mode, arm2) )
422
433
|| ( check_arm_is_some_or_ok ( cx, mode, arm2) && check_arm_is_none_or_err ( cx, mode, arm1) )
0 commit comments