@@ -10,8 +10,8 @@ use rustc_middle::ty;
1010use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
1111
1212declare_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.
1515 ///
1616 /// **Why is this bad?** Redundant code.
1717 ///
@@ -29,7 +29,7 @@ declare_clippy_lint! {
2929 /// ```
3030 pub USELESS_CONVERSION ,
3131 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"
3333}
3434
3535#[ derive( Default ) ]
@@ -39,6 +39,7 @@ pub struct UselessConversion {
3939
4040impl_lint_pass ! ( UselessConversion => [ USELESS_CONVERSION ] ) ;
4141
42+ #[ allow( clippy:: too_many_lines) ]
4243impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UselessConversion {
4344 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr < ' _ > ) {
4445 if e. span . from_expansion ( ) {
@@ -66,7 +67,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
6667 let b = cx. tables . expr_ty ( & args[ 0 ] ) ;
6768 if same_tys ( cx, a, b) {
6869 let sugg = snippet_with_macro_callsite ( cx, args[ 0 ] . span , "<expr>" ) . to_string ( ) ;
69-
7070 span_lint_and_sugg (
7171 cx,
7272 USELESS_CONVERSION ,
@@ -94,6 +94,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
9494 ) ;
9595 }
9696 }
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+ }
97118 } ,
98119
99120 ExprKind :: Call ( ref path, ref args) => {
@@ -109,7 +130,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
109130 if match_def_path( cx, def_id, & paths:: TRY_FROM ) ;
110131 if is_type_diagnostic_item( cx, a, sym!( result_type) ) ;
111132 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 ( ) ;
113134 if same_tys( cx, a_type, b) ;
114135
115136 then {
@@ -125,8 +146,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
125146 }
126147 }
127148
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 {
130154 let sugg = snippet( cx, args[ 0 ] . span. source_callsite( ) , "<expr>" ) . into_owned( ) ;
131155 let sugg_msg =
132156 format!( "consider removing `{}()`" , snippet( cx, path. span, "From::from" ) ) ;
0 commit comments