11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: source:: snippet_with_applicability;
3- use clippy_utils:: ty:: is_type_diagnostic_item ;
3+ use clippy_utils:: ty:: option_arg_ty ;
44use clippy_utils:: { get_parent_expr, is_res_lang_ctor, path_res} ;
55use rustc_errors:: Applicability ;
66use rustc_hir:: LangItem :: ResultErr ;
@@ -28,25 +28,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
2828 && is_res_lang_ctor ( cx, path_res ( cx, err_fun) , ResultErr )
2929 && let Some ( return_ty) = find_return_type ( cx, & expr. kind )
3030 {
31- let prefix;
32- let suffix;
33- let err_ty;
34-
35- if let Some ( ty) = result_error_type ( cx, return_ty) {
36- prefix = "Err(" ;
37- suffix = ")" ;
38- err_ty = ty;
31+ let ( prefix, suffix, err_ty) = if let Some ( ty) = result_error_type ( cx, return_ty) {
32+ ( "Err(" , ")" , ty)
3933 } else if let Some ( ty) = poll_result_error_type ( cx, return_ty) {
40- prefix = "Poll::Ready(Err(" ;
41- suffix = "))" ;
42- err_ty = ty;
34+ ( "Poll::Ready(Err(" , "))" , ty)
4335 } else if let Some ( ty) = poll_option_result_error_type ( cx, return_ty) {
44- prefix = "Poll::Ready(Some(Err(" ;
45- suffix = ")))" ;
46- err_ty = ty;
36+ ( "Poll::Ready(Some(Err(" , ")))" , ty)
4737 } else {
4838 return ;
49- }
39+ } ;
5040
5141 span_lint_and_then (
5242 cx,
@@ -88,8 +78,8 @@ fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> O
8878
8979/// Extracts the error type from Result<T, E>.
9080fn result_error_type < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> Option < Ty < ' tcx > > {
91- if let ty:: Adt ( _ , subst) = ty. kind ( )
92- && is_type_diagnostic_item ( cx , ty , sym:: Result )
81+ if let ty:: Adt ( def , subst) = ty. kind ( )
82+ && cx . tcx . is_diagnostic_item ( sym:: Result , def . did ( ) )
9383 {
9484 Some ( subst. type_at ( 1 ) )
9585 } else {
@@ -101,11 +91,9 @@ fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
10191fn poll_result_error_type < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> Option < Ty < ' tcx > > {
10292 if let ty:: Adt ( def, subst) = ty. kind ( )
10393 && cx. tcx . lang_items ( ) . get ( LangItem :: Poll ) == Some ( def. did ( ) )
104- && let ready_ty = subst. type_at ( 0 )
105- && let ty:: Adt ( ready_def, ready_subst) = ready_ty. kind ( )
106- && cx. tcx . is_diagnostic_item ( sym:: Result , ready_def. did ( ) )
10794 {
108- Some ( ready_subst. type_at ( 1 ) )
95+ let ready_ty = subst. type_at ( 0 ) ;
96+ result_error_type ( cx, ready_ty)
10997 } else {
11098 None
11199 }
@@ -116,13 +104,9 @@ fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) ->
116104 if let ty:: Adt ( def, subst) = ty. kind ( )
117105 && cx. tcx . lang_items ( ) . get ( LangItem :: Poll ) == Some ( def. did ( ) )
118106 && let ready_ty = subst. type_at ( 0 )
119- && let ty:: Adt ( ready_def, ready_subst) = ready_ty. kind ( )
120- && cx. tcx . is_diagnostic_item ( sym:: Option , ready_def. did ( ) )
121- && let some_ty = ready_subst. type_at ( 0 )
122- && let ty:: Adt ( some_def, some_subst) = some_ty. kind ( )
123- && cx. tcx . is_diagnostic_item ( sym:: Result , some_def. did ( ) )
107+ && let Some ( some_ty) = option_arg_ty ( cx, ready_ty)
124108 {
125- Some ( some_subst . type_at ( 1 ) )
109+ result_error_type ( cx , some_ty )
126110 } else {
127111 None
128112 }
0 commit comments