@@ -6,6 +6,7 @@ use rustc_hir::*;
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;
9
+ use std:: borrow:: Cow ;
9
10
10
11
declare_clippy_lint ! {
11
12
/// **What it does:** Checking for using of From or TryFrom trait as a generic bound.
@@ -37,7 +38,7 @@ declare_lint_pass!(FromInsteadOfInto => [FROM_INSTEAD_OF_INTO]);
37
38
38
39
impl LateLintPass < ' tcx > for FromInsteadOfInto {
39
40
fn check_where_predicate ( & mut self , cx : & LateContext < ' tcx > , wp : & ' tcx WherePredicate < ' tcx > ) {
40
- let is_target_generic_bound = | b : & GenericBound < ' _ > | {
41
+ fn is_target_generic_bound ( cx : & LateContext < ' tcx > , b : & GenericBound < ' _ > ) -> bool {
41
42
if_chain ! {
42
43
if let Some ( r) = b. trait_ref( ) ;
43
44
if let Some ( def_id) = r. trait_def_id( ) ;
@@ -48,20 +49,56 @@ impl LateLintPass<'tcx> for FromInsteadOfInto {
48
49
false
49
50
}
50
51
}
51
- } ;
52
+ }
53
+
54
+ fn get_reduced_bounds_str (
55
+ cx : & LateContext < ' tcx > ,
56
+ position : usize ,
57
+ bounds : GenericBounds < ' tcx > ,
58
+ ) -> Cow < ' tcx , str > {
59
+ let before;
60
+ if position == 0 {
61
+ before = None ;
62
+ } else {
63
+ let first_bound = & bounds[ 0 ] ;
64
+ let previous_bound = & bounds[ position - 1 ] ;
65
+ before = Some ( snippet (
66
+ cx,
67
+ first_bound. span ( ) . with_hi ( previous_bound. span ( ) . hi ( ) ) ,
68
+ ".." ,
69
+ ) ) ;
70
+ }
71
+
72
+ let after;
73
+ let last_position = bounds. len ( ) - 1 ;
74
+ if position == last_position {
75
+ after = None ;
76
+ } else {
77
+ let last_bound = & bounds[ last_position] ;
78
+ let after_bound = & bounds[ position + 1 ] ;
79
+ after = Some ( snippet ( cx, after_bound. span ( ) . with_hi ( last_bound. span ( ) . hi ( ) ) , ".." ) ) ;
80
+ }
81
+
82
+ match ( before, after) {
83
+ ( None , None ) => unreachable ! ( ) ,
84
+ ( Some ( b) , None ) => b,
85
+ ( None , Some ( a) ) => a,
86
+ ( Some ( b) , Some ( a) ) => b + " + " + a,
87
+ }
88
+ }
89
+
52
90
match wp {
53
91
WherePredicate :: BoundPredicate ( wbp) => {
54
92
if_chain ! {
55
93
let bounds = wbp. bounds;
56
- if let Some ( position) = bounds. iter( ) . position( |b| is_target_generic_bound( b) ) ;
94
+ if let Some ( position) = bounds. iter( ) . position( |b| is_target_generic_bound( cx , b) ) ;
57
95
let target_bound = & bounds[ position] ;
58
96
if let Some ( tr_ref) = target_bound. trait_ref( ) ;
59
97
if let Some ( def_id) = tr_ref. trait_def_id( ) ;
60
98
if let Some ( last_seg) = tr_ref. path. segments. last( ) ;
61
99
if let Some ( generic_arg) = last_seg. args( ) . args. first( ) ;
62
100
if let Some ( bounded_ty) = snippet_opt( cx, wbp. bounded_ty. span) ;
63
101
if let Some ( generic_arg_of_from_or_try_from) = snippet_opt( cx, generic_arg. span( ) ) ;
64
- // if wbp.bounds.len() == 1;
65
102
then {
66
103
let replace_trait_name;
67
104
let target_trait_name;
@@ -84,33 +121,8 @@ impl LateLintPass<'tcx> for FromInsteadOfInto {
84
121
if bounds. len( ) == 1 {
85
122
sugg = extracted_where_predicate;
86
123
} else {
87
- let before;
88
- if position == 0 {
89
- before = None ;
90
- } else {
91
- let first_bound = & bounds[ 0 ] ;
92
- let previous_bound = & bounds[ position-1 ] ;
93
- before = Some ( snippet( cx, first_bound. span( ) . with_hi( previous_bound. span( ) . hi( ) ) , ".." ) ) ;
94
- }
95
-
96
- let after;
97
- let last_position = bounds. len( ) - 1 ;
98
- if position == last_position {
99
- after = None ;
100
- } else {
101
- let last_bound = & bounds[ last_position] ;
102
- let after_bound = & bounds[ position + 1 ] ;
103
- after = Some ( snippet( cx, after_bound. span( ) . with_hi( last_bound. span( ) . hi( ) ) , ".." ) ) ;
104
- }
105
-
106
- let bounds_str = match ( before, after) {
107
- ( None , None ) => unreachable!( ) ,
108
- ( Some ( b) , None ) => b,
109
- ( None , Some ( a) ) => a,
110
- ( Some ( b) , Some ( a) ) => b + " + " + a,
111
- } ;
112
-
113
- sugg = format!( "{}, {}: {}" , extracted_where_predicate, bounded_ty, bounds_str) ;
124
+ let bounds = get_reduced_bounds_str( cx, position, bounds) ;
125
+ sugg = format!( "{}, {}: {}" , extracted_where_predicate, bounded_ty, bounds) ;
114
126
}
115
127
span_lint_and_sugg(
116
128
cx,
0 commit comments