1
1
use clippy_utils:: diagnostics:: span_lint_and_then;
2
2
use clippy_utils:: source:: snippet_with_applicability;
3
- use clippy_utils:: ty:: is_type_diagnostic_item ;
3
+ use clippy_utils:: ty:: option_arg_ty ;
4
4
use clippy_utils:: { get_parent_expr, is_res_lang_ctor, path_res} ;
5
5
use rustc_errors:: Applicability ;
6
6
use rustc_hir:: LangItem :: ResultErr ;
@@ -28,25 +28,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
28
28
&& is_res_lang_ctor ( cx, path_res ( cx, err_fun) , ResultErr )
29
29
&& let Some ( return_ty) = find_return_type ( cx, & expr. kind )
30
30
{
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)
39
33
} 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)
43
35
} 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)
47
37
} else {
48
38
return ;
49
- }
39
+ } ;
50
40
51
41
span_lint_and_then (
52
42
cx,
@@ -88,8 +78,8 @@ fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> O
88
78
89
79
/// Extracts the error type from Result<T, E>.
90
80
fn 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 ( ) )
93
83
{
94
84
Some ( subst. type_at ( 1 ) )
95
85
} else {
@@ -101,11 +91,9 @@ fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
101
91
fn poll_result_error_type < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> Option < Ty < ' tcx > > {
102
92
if let ty:: Adt ( def, subst) = ty. kind ( )
103
93
&& 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 ( ) )
107
94
{
108
- Some ( ready_subst. type_at ( 1 ) )
95
+ let ready_ty = subst. type_at ( 0 ) ;
96
+ result_error_type ( cx, ready_ty)
109
97
} else {
110
98
None
111
99
}
@@ -116,13 +104,9 @@ fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) ->
116
104
if let ty:: Adt ( def, subst) = ty. kind ( )
117
105
&& cx. tcx . lang_items ( ) . get ( LangItem :: Poll ) == Some ( def. did ( ) )
118
106
&& 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)
124
108
{
125
- Some ( some_subst . type_at ( 1 ) )
109
+ result_error_type ( cx , some_ty )
126
110
} else {
127
111
None
128
112
}
0 commit comments