@@ -10,8 +10,8 @@ use rustc_middle::ty;
10
10
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
11
11
12
12
declare_clippy_lint ! {
13
- /// **What it does:** Checks for `Into`, `From`, `TryFrom`,`IntoIter` calls that useless converts
14
- /// to the same type as caller.
13
+ /// **What it does:** Checks for `Into`, `TryInto`, ` From`, `TryFrom`,`IntoIter` calls
14
+ /// that useless converts to the same type as caller.
15
15
///
16
16
/// **Why is this bad?** Redundant code.
17
17
///
@@ -29,7 +29,7 @@ declare_clippy_lint! {
29
29
/// ```
30
30
pub USELESS_CONVERSION ,
31
31
complexity,
32
- "calls to `Into`, `From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
32
+ "calls to `Into`, `TryInto`, ` From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
33
33
}
34
34
35
35
#[ derive( Default ) ]
@@ -39,6 +39,7 @@ pub struct UselessConversion {
39
39
40
40
impl_lint_pass ! ( UselessConversion => [ USELESS_CONVERSION ] ) ;
41
41
42
+ #[ allow( clippy:: too_many_lines) ]
42
43
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UselessConversion {
43
44
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr < ' _ > ) {
44
45
if e. span . from_expansion ( ) {
@@ -66,7 +67,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
66
67
let b = cx. tables . expr_ty ( & args[ 0 ] ) ;
67
68
if same_tys ( cx, a, b) {
68
69
let sugg = snippet_with_macro_callsite ( cx, args[ 0 ] . span , "<expr>" ) . to_string ( ) ;
69
-
70
70
span_lint_and_sugg (
71
71
cx,
72
72
USELESS_CONVERSION ,
@@ -94,6 +94,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
94
94
) ;
95
95
}
96
96
}
97
+ if match_trait_method ( cx, e, & paths:: TRY_INTO_TRAIT ) && & * name. ident . as_str ( ) == "try_into" {
98
+ if_chain ! {
99
+ let a = cx. tables. expr_ty( e) ;
100
+ let b = cx. tables. expr_ty( & args[ 0 ] ) ;
101
+ if is_type_diagnostic_item( cx, a, sym!( result_type) ) ;
102
+ if let ty:: Adt ( _, substs) = a. kind;
103
+ if let Some ( a_type) = substs. types( ) . next( ) ;
104
+ if same_tys( cx, a_type, b) ;
105
+
106
+ then {
107
+ span_lint_and_help(
108
+ cx,
109
+ USELESS_CONVERSION ,
110
+ e. span,
111
+ "Useless conversion to the same type" ,
112
+ None ,
113
+ "consider removing `.try_into()`" ,
114
+ ) ;
115
+ }
116
+ }
117
+ }
97
118
} ,
98
119
99
120
ExprKind :: Call ( ref path, ref args) => {
@@ -109,7 +130,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
109
130
if match_def_path( cx, def_id, & paths:: TRY_FROM ) ;
110
131
if is_type_diagnostic_item( cx, a, sym!( result_type) ) ;
111
132
if let ty:: Adt ( _, substs) = a. kind;
112
- if let Some ( a_type) = substs. types( ) . nth ( 0 ) ;
133
+ if let Some ( a_type) = substs. types( ) . next ( ) ;
113
134
if same_tys( cx, a_type, b) ;
114
135
115
136
then {
@@ -125,8 +146,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
125
146
}
126
147
}
127
148
128
- if match_def_path( cx, def_id, & paths:: FROM_FROM ) {
129
- if same_tys( cx, a, b) {
149
+ if_chain! {
150
+ if match_def_path( cx, def_id, & paths:: FROM_FROM ) ;
151
+ if same_tys( cx, a, b) ;
152
+
153
+ then {
130
154
let sugg = snippet( cx, args[ 0 ] . span. source_callsite( ) , "<expr>" ) . into_owned( ) ;
131
155
let sugg_msg =
132
156
format!( "consider removing `{}()`" , snippet( cx, path. span, "From::from" ) ) ;
0 commit comments