@@ -12,15 +12,15 @@ use super::UNNECESSARY_FALLIBLE_CONVERSIONS;
12
12
13
13
/// What function is being called and whether that call is written as a method call or a function
14
14
/// call
15
- #[ derive( Copy , Clone ) ]
15
+ #[ derive( Clone ) ]
16
16
#[ expect( clippy:: enum_variant_names) ]
17
17
enum FunctionKind {
18
18
/// `T::try_from(U)`
19
- TryFromFunction ,
19
+ TryFromFunction ( Option < Vec < Span > > ) ,
20
20
/// `t.try_into()`
21
21
TryIntoMethod ,
22
22
/// `U::try_into(t)`
23
- TryIntoFunction ,
23
+ TryIntoFunction ( Option < Vec < Span > > ) ,
24
24
}
25
25
26
26
fn check < ' tcx > (
@@ -29,15 +29,14 @@ fn check<'tcx>(
29
29
node_args : ty:: GenericArgsRef < ' tcx > ,
30
30
kind : FunctionKind ,
31
31
primary_span : Span ,
32
- qpath : Option < & QPath < ' _ > > ,
33
32
) {
34
33
if let & [ self_ty, other_ty] = node_args. as_slice ( )
35
34
// useless_conversion already warns `T::try_from(T)`, so ignore it here
36
35
&& self_ty != other_ty
37
36
&& let Some ( self_ty) = self_ty. as_type ( )
38
37
&& let Some ( from_into_trait) = cx. tcx . get_diagnostic_item ( match kind {
39
- FunctionKind :: TryFromFunction => sym:: From ,
40
- FunctionKind :: TryIntoMethod | FunctionKind :: TryIntoFunction => sym:: Into ,
38
+ FunctionKind :: TryFromFunction ( _ ) => sym:: From ,
39
+ FunctionKind :: TryIntoMethod | FunctionKind :: TryIntoFunction ( _ ) => sym:: Into ,
41
40
} )
42
41
// If `T: TryFrom<U>` and `T: From<U>` both exist, then that means that the `TryFrom`
43
42
// _must_ be from the blanket impl and cannot have been manually implemented
@@ -70,21 +69,12 @@ fn check<'tcx>(
70
69
primary_span
71
70
} ;
72
71
73
- let qpath_spans = qpath. and_then ( |qpath| match qpath {
74
- QPath :: Resolved ( _, path) => {
75
- let segments = path. segments . iter ( ) . map ( |seg| seg. ident ) . collect :: < Vec < _ > > ( ) ;
76
- ( segments. len ( ) == 2 ) . then ( || vec ! [ segments[ 0 ] . span, segments[ 1 ] . span] )
77
- } ,
78
- QPath :: TypeRelative ( _, seg) => Some ( vec ! [ seg. ident. span] ) ,
79
- QPath :: LangItem ( _, _) => unreachable ! ( "`TryFrom` and `TryInto` are not lang items" ) ,
80
- } ) ;
81
-
82
- let ( source_ty, target_ty, sugg, applicability) = match ( kind, & qpath_spans, parent_unwrap_call) {
83
- ( FunctionKind :: TryIntoMethod , _, Some ( unwrap_span) ) => {
72
+ let ( source_ty, target_ty, sugg, applicability) = match ( kind, parent_unwrap_call) {
73
+ ( FunctionKind :: TryIntoMethod , Some ( unwrap_span) ) => {
84
74
let sugg = vec ! [ ( primary_span, String :: from( "into" ) ) , ( unwrap_span, String :: new( ) ) ] ;
85
75
( self_ty, other_ty, sugg, Applicability :: MachineApplicable )
86
76
} ,
87
- ( FunctionKind :: TryFromFunction , Some ( spans) , Some ( unwrap_span) ) => {
77
+ ( FunctionKind :: TryFromFunction ( Some ( spans) ) , Some ( unwrap_span) ) => {
88
78
let sugg = match spans. len ( ) {
89
79
1 => vec ! [ ( spans[ 0 ] , String :: from( "from" ) ) , ( unwrap_span, String :: new( ) ) ] ,
90
80
2 => vec ! [
@@ -96,7 +86,7 @@ fn check<'tcx>(
96
86
} ;
97
87
( other_ty, self_ty, sugg, Applicability :: MachineApplicable )
98
88
} ,
99
- ( FunctionKind :: TryIntoFunction , Some ( spans) , Some ( unwrap_span) ) => {
89
+ ( FunctionKind :: TryIntoFunction ( Some ( spans) ) , Some ( unwrap_span) ) => {
100
90
let sugg = match spans. len ( ) {
101
91
1 => vec ! [ ( spans[ 0 ] , String :: from( "into" ) ) , ( unwrap_span, String :: new( ) ) ] ,
102
92
2 => vec ! [
@@ -108,15 +98,15 @@ fn check<'tcx>(
108
98
} ;
109
99
( self_ty, other_ty, sugg, Applicability :: MachineApplicable )
110
100
} ,
111
- ( FunctionKind :: TryFromFunction , _ , _) => {
101
+ ( FunctionKind :: TryFromFunction ( _ ) , _) => {
112
102
let sugg = vec ! [ ( primary_span, String :: from( "From::from" ) ) ] ;
113
103
( other_ty, self_ty, sugg, Applicability :: Unspecified )
114
104
} ,
115
- ( FunctionKind :: TryIntoFunction , _ , _) => {
105
+ ( FunctionKind :: TryIntoFunction ( _ ) , _) => {
116
106
let sugg = vec ! [ ( primary_span, String :: from( "Into::into" ) ) ] ;
117
107
( self_ty, other_ty, sugg, Applicability :: Unspecified )
118
108
} ,
119
- ( FunctionKind :: TryIntoMethod , _, _ ) => {
109
+ ( FunctionKind :: TryIntoMethod , _) => {
120
110
let sugg = vec ! [ ( primary_span, String :: from( "into" ) ) ] ;
121
111
( self_ty, other_ty, sugg, Applicability :: Unspecified )
122
112
} ,
@@ -147,7 +137,6 @@ pub(super) fn check_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
147
137
cx. typeck_results ( ) . node_args ( expr. hir_id ) ,
148
138
FunctionKind :: TryIntoMethod ,
149
139
path. ident . span ,
150
- None ,
151
140
) ;
152
141
}
153
142
}
@@ -160,17 +149,25 @@ pub(super) fn check_function(cx: &LateContext<'_>, expr: &Expr<'_>, callee: &Exp
160
149
&& let Some ( item_def_id) = cx. qpath_res ( qpath, callee. hir_id ) . opt_def_id ( )
161
150
&& let Some ( trait_def_id) = cx. tcx . trait_of_item ( item_def_id)
162
151
{
152
+ let qpath_spans = match qpath {
153
+ QPath :: Resolved ( _, path) => {
154
+ let segments = path. segments . iter ( ) . map ( |seg| seg. ident ) . collect :: < Vec < _ > > ( ) ;
155
+ ( segments. len ( ) == 2 ) . then ( || vec ! [ segments[ 0 ] . span, segments[ 1 ] . span] )
156
+ } ,
157
+ QPath :: TypeRelative ( _, seg) => Some ( vec ! [ seg. ident. span] ) ,
158
+ QPath :: LangItem ( _, _) => unreachable ! ( "`TryFrom` and `TryInto` are not lang items" ) ,
159
+ } ;
160
+
163
161
check (
164
162
cx,
165
163
expr,
166
164
cx. typeck_results ( ) . node_args ( callee. hir_id ) ,
167
165
match cx. tcx . get_diagnostic_name ( trait_def_id) {
168
- Some ( sym:: TryFrom ) => FunctionKind :: TryFromFunction ,
169
- Some ( sym:: TryInto ) => FunctionKind :: TryIntoFunction ,
166
+ Some ( sym:: TryFrom ) => FunctionKind :: TryFromFunction ( qpath_spans ) ,
167
+ Some ( sym:: TryInto ) => FunctionKind :: TryIntoFunction ( qpath_spans ) ,
170
168
_ => return ,
171
169
} ,
172
170
callee. span ,
173
- Some ( qpath) ,
174
171
) ;
175
172
}
176
173
}
0 commit comments