@@ -2,7 +2,7 @@ use crate::utils::span_lint_and_sugg;
2
2
use crate :: utils:: { snippet, snippet_opt} ;
3
3
use if_chain:: if_chain;
4
4
use rustc_errors:: Applicability ;
5
- use rustc_hir:: * ;
5
+ use rustc_hir:: { GenericBound , GenericBounds , WherePredicate } ;
6
6
use rustc_lint:: { LateContext , LateLintPass } ;
7
7
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
8
8
use rustc_span:: symbol:: sym;
@@ -87,57 +87,54 @@ impl LateLintPass<'tcx> for FromInsteadOfInto {
87
87
}
88
88
}
89
89
90
- match wp {
91
- WherePredicate :: BoundPredicate ( wbp) => {
92
- if_chain ! {
93
- let bounds = wbp. bounds;
94
- if let Some ( position) = bounds. iter( ) . position( |b| is_target_generic_bound( cx, b) ) ;
95
- let target_bound = & bounds[ position] ;
96
- if let Some ( tr_ref) = target_bound. trait_ref( ) ;
97
- if let Some ( def_id) = tr_ref. trait_def_id( ) ;
98
- if let Some ( last_seg) = tr_ref. path. segments. last( ) ;
99
- if let Some ( generic_arg) = last_seg. args( ) . args. first( ) ;
100
- if let Some ( bounded_ty) = snippet_opt( cx, wbp. bounded_ty. span) ;
101
- if let Some ( generic_arg_of_from_or_try_from) = snippet_opt( cx, generic_arg. span( ) ) ;
102
- then {
103
- let replace_trait_name;
104
- let target_trait_name;
105
- if cx. tcx. is_diagnostic_item( sym:: from_trait, def_id) {
106
- replace_trait_name = "Into" ;
107
- target_trait_name = "From" ;
108
- } else if cx. tcx. is_diagnostic_item( sym:: try_from_trait, def_id) {
109
- replace_trait_name = "TryInto" ;
110
- target_trait_name = "TryFrom" ;
111
- } else {
112
- replace_trait_name = "" ;
113
- target_trait_name = "" ;
114
- }
90
+ if let WherePredicate :: BoundPredicate ( wbp) = wp {
91
+ if_chain ! {
92
+ let bounds = wbp. bounds;
93
+ if let Some ( position) = bounds. iter( ) . position( |b| is_target_generic_bound( cx, b) ) ;
94
+ let target_bound = & bounds[ position] ;
95
+ if let Some ( tr_ref) = target_bound. trait_ref( ) ;
96
+ if let Some ( def_id) = tr_ref. trait_def_id( ) ;
97
+ if let Some ( last_seg) = tr_ref. path. segments. last( ) ;
98
+ if let Some ( generic_arg) = last_seg. args( ) . args. first( ) ;
99
+ if let Some ( bounded_ty) = snippet_opt( cx, wbp. bounded_ty. span) ;
100
+ if let Some ( generic_arg_of_from_or_try_from) = snippet_opt( cx, generic_arg. span( ) ) ;
101
+ then {
102
+ let replace_trait_name;
103
+ let target_trait_name;
104
+ if cx. tcx. is_diagnostic_item( sym:: from_trait, def_id) {
105
+ replace_trait_name = "Into" ;
106
+ target_trait_name = "From" ;
107
+ } else if cx. tcx. is_diagnostic_item( sym:: try_from_trait, def_id) {
108
+ replace_trait_name = "TryInto" ;
109
+ target_trait_name = "TryFrom" ;
110
+ } else {
111
+ replace_trait_name = "" ;
112
+ target_trait_name = "" ;
113
+ }
115
114
116
- if !replace_trait_name. is_empty( ) && !target_trait_name. is_empty( ) {
117
- let message = format!( "{} trait is preferable than {} as a generic bound" , replace_trait_name, target_trait_name) ;
115
+ if !replace_trait_name. is_empty( ) && !target_trait_name. is_empty( ) {
116
+ let message = format!( "{} trait is preferable than {} as a generic bound" , replace_trait_name, target_trait_name) ;
118
117
119
- let extracted_where_predicate = format!( "{}: {}<{}>" , generic_arg_of_from_or_try_from, replace_trait_name, bounded_ty) ;
120
- let sugg;
121
- if bounds. len( ) == 1 {
122
- sugg = extracted_where_predicate;
123
- } else {
124
- let bounds = get_reduced_bounds_str( cx, position, bounds) ;
125
- sugg = format!( "{}, {}: {}" , extracted_where_predicate, bounded_ty, bounds) ;
126
- }
127
- span_lint_and_sugg(
128
- cx,
129
- FROM_INSTEAD_OF_INTO ,
130
- wp. span( ) ,
131
- & message,
132
- "try" ,
133
- sugg,
134
- Applicability :: MaybeIncorrect
135
- ) ;
118
+ let extracted_where_predicate = format!( "{}: {}<{}>" , generic_arg_of_from_or_try_from, replace_trait_name, bounded_ty) ;
119
+ let sugg;
120
+ if bounds. len( ) == 1 {
121
+ sugg = extracted_where_predicate;
122
+ } else {
123
+ let bounds = get_reduced_bounds_str( cx, position, bounds) ;
124
+ sugg = format!( "{}, {}: {}" , extracted_where_predicate, bounded_ty, bounds) ;
136
125
}
126
+ span_lint_and_sugg(
127
+ cx,
128
+ FROM_INSTEAD_OF_INTO ,
129
+ wp. span( ) ,
130
+ & message,
131
+ "try" ,
132
+ sugg,
133
+ Applicability :: MaybeIncorrect
134
+ ) ;
137
135
}
138
136
}
139
- } ,
140
- _ => ( ) ,
141
- } ;
137
+ }
138
+ }
142
139
}
143
140
}
0 commit comments