@@ -820,9 +820,9 @@ impl Offset {
820
820
}
821
821
}
822
822
823
- struct FixedOffsetVar < ' hir > {
824
- var : & ' hir Expr < ' hir > ,
825
- offset : Offset ,
823
+ struct IndexExpr < ' hir > {
824
+ base : & ' hir Expr < ' hir > ,
825
+ idx_offset : Offset ,
826
826
}
827
827
828
828
fn is_slice_like < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' _ > ) -> bool {
@@ -845,35 +845,35 @@ fn fetch_cloned_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
845
845
}
846
846
}
847
847
848
- fn get_offset < ' tcx > ( cx : & LateContext < ' tcx > , idx : & Expr < ' _ > , var : HirId ) -> Option < Offset > {
849
- fn extract_offset < ' tcx > ( cx : & LateContext < ' tcx > , e : & Expr < ' _ > , var : HirId ) -> Option < String > {
848
+ fn get_offset < ' tcx > ( cx : & LateContext < ' tcx > , idx : & Expr < ' _ > , start : HirId ) -> Option < Offset > {
849
+ fn extract_offset < ' tcx > ( cx : & LateContext < ' tcx > , e : & Expr < ' _ > , start : HirId ) -> Option < String > {
850
850
match & e. kind {
851
851
ExprKind :: Lit ( l) => match l. node {
852
852
ast:: LitKind :: Int ( x, _ty) => Some ( x. to_string ( ) ) ,
853
853
_ => None ,
854
854
} ,
855
- ExprKind :: Path ( ..) if !same_var ( cx, e, var ) => Some ( snippet_opt ( cx, e. span ) . unwrap_or_else ( || "??" . into ( ) ) ) ,
855
+ ExprKind :: Path ( ..) if !same_var ( cx, e, start ) => Some ( snippet_opt ( cx, e. span ) . unwrap_or_else ( || "??" . into ( ) ) ) ,
856
856
_ => None ,
857
857
}
858
858
}
859
859
860
860
match idx. kind {
861
861
ExprKind :: Binary ( op, lhs, rhs) => match op. node {
862
862
BinOpKind :: Add => {
863
- let offset_opt = if same_var ( cx, lhs, var ) {
864
- extract_offset ( cx, rhs, var )
865
- } else if same_var ( cx, rhs, var ) {
866
- extract_offset ( cx, lhs, var )
863
+ let offset_opt = if same_var ( cx, lhs, start ) {
864
+ extract_offset ( cx, rhs, start )
865
+ } else if same_var ( cx, rhs, start ) {
866
+ extract_offset ( cx, lhs, start )
867
867
} else {
868
868
None
869
869
} ;
870
870
871
871
offset_opt. map ( Offset :: positive)
872
872
} ,
873
- BinOpKind :: Sub if same_var ( cx, lhs, var ) => extract_offset ( cx, rhs, var ) . map ( Offset :: negative) ,
873
+ BinOpKind :: Sub if same_var ( cx, lhs, start ) => extract_offset ( cx, rhs, start ) . map ( Offset :: negative) ,
874
874
_ => None ,
875
875
} ,
876
- ExprKind :: Path ( ..) if same_var ( cx, idx, var ) => Some ( Offset :: positive ( "0" . into ( ) ) ) ,
876
+ ExprKind :: Path ( ..) if same_var ( cx, idx, start ) => Some ( Offset :: positive ( "0" . into ( ) ) ) ,
877
877
_ => None ,
878
878
}
879
879
}
@@ -916,8 +916,8 @@ fn build_manual_memcpy_suggestion<'tcx>(
916
916
start : & Expr < ' _ > ,
917
917
end : & Expr < ' _ > ,
918
918
limits : ast:: RangeLimits ,
919
- dst_var : FixedOffsetVar < ' _ > ,
920
- src_var : FixedOffsetVar < ' _ > ,
919
+ dst : IndexExpr < ' _ > ,
920
+ src : IndexExpr < ' _ > ,
921
921
) -> String {
922
922
fn print_sum ( arg1 : & str , arg2 : & Offset ) -> String {
923
923
match ( arg1, & arg2. value [ ..] , arg2. sign ) {
@@ -944,13 +944,13 @@ fn build_manual_memcpy_suggestion<'tcx>(
944
944
}
945
945
}
946
946
947
- let print_limit = |end : & Expr < ' _ > , offset : Offset , var : & Expr < ' _ > | {
947
+ let print_limit = |end : & Expr < ' _ > , offset : Offset , base : & Expr < ' _ > | {
948
948
if_chain ! {
949
949
if let ExprKind :: MethodCall ( method, _, len_args, _) = end. kind;
950
950
if method. ident. name == sym!( len) ;
951
951
if len_args. len( ) == 1 ;
952
952
if let Some ( arg) = len_args. get( 0 ) ;
953
- if var_def_id( cx, arg) == var_def_id( cx, var ) ;
953
+ if var_def_id( cx, arg) == var_def_id( cx, base ) ;
954
954
then {
955
955
match offset. sign {
956
956
OffsetSign :: Negative => format!( "({} - {})" , snippet( cx, end. span, "<src>.len()" ) , offset. value) ,
@@ -971,25 +971,26 @@ fn build_manual_memcpy_suggestion<'tcx>(
971
971
} ;
972
972
973
973
let start_str = snippet ( cx, start. span , "" ) . to_string ( ) ;
974
- let dst_offset = print_offset ( & start_str, & dst_var . offset ) ;
975
- let dst_limit = print_limit ( end, dst_var . offset , dst_var . var ) ;
976
- let src_offset = print_offset ( & start_str, & src_var . offset ) ;
977
- let src_limit = print_limit ( end, src_var . offset , src_var . var ) ;
974
+ let dst_offset = print_offset ( & start_str, & dst . idx_offset ) ;
975
+ let dst_limit = print_limit ( end, dst . idx_offset , dst . base ) ;
976
+ let src_offset = print_offset ( & start_str, & src . idx_offset ) ;
977
+ let src_limit = print_limit ( end, src . idx_offset , src . base ) ;
978
978
979
- let dst_var_name = snippet_opt ( cx, dst_var . var . span ) . unwrap_or_else ( || "???" . into ( ) ) ;
980
- let src_var_name = snippet_opt ( cx, src_var . var . span ) . unwrap_or_else ( || "???" . into ( ) ) ;
979
+ let dst_base_str = snippet_opt ( cx, dst . base . span ) . unwrap_or_else ( || "???" . into ( ) ) ;
980
+ let src_base_str = snippet_opt ( cx, src . base . span ) . unwrap_or_else ( || "???" . into ( ) ) ;
981
981
982
982
let dst = if dst_offset == "" && dst_limit == "" {
983
- dst_var_name
983
+ dst_base_str
984
984
} else {
985
- format ! ( "{}[{}..{}]" , dst_var_name , dst_offset, dst_limit)
985
+ format ! ( "{}[{}..{}]" , dst_base_str , dst_offset, dst_limit)
986
986
} ;
987
987
988
988
format ! (
989
989
"{}.clone_from_slice(&{}[{}..{}])" ,
990
- dst, src_var_name , src_offset, src_limit
990
+ dst, src_base_str , src_offset, src_limit
991
991
)
992
992
}
993
+
993
994
/// Checks for for loops that sequentially copy items from one slice-like
994
995
/// object to another.
995
996
fn detect_manual_memcpy < ' tcx > (
@@ -1014,18 +1015,18 @@ fn detect_manual_memcpy<'tcx>(
1014
1015
o. and_then ( |( lhs, rhs) | {
1015
1016
let rhs = fetch_cloned_expr ( rhs) ;
1016
1017
if_chain ! {
1017
- if let ExprKind :: Index ( seqexpr_left , idx_left) = lhs. kind;
1018
- if let ExprKind :: Index ( seqexpr_right , idx_right) = rhs. kind;
1019
- if is_slice_like( cx, cx. typeck_results( ) . expr_ty( seqexpr_left ) )
1020
- && is_slice_like( cx, cx. typeck_results( ) . expr_ty( seqexpr_right ) ) ;
1018
+ if let ExprKind :: Index ( base_left , idx_left) = lhs. kind;
1019
+ if let ExprKind :: Index ( base_right , idx_right) = rhs. kind;
1020
+ if is_slice_like( cx, cx. typeck_results( ) . expr_ty( base_left ) )
1021
+ && is_slice_like( cx, cx. typeck_results( ) . expr_ty( base_right ) ) ;
1021
1022
if let Some ( offset_left) = get_offset( cx, & idx_left, canonical_id) ;
1022
1023
if let Some ( offset_right) = get_offset( cx, & idx_right, canonical_id) ;
1023
1024
1024
1025
// Source and destination must be different
1025
- if var_def_id( cx, seqexpr_left ) != var_def_id( cx, seqexpr_right ) ;
1026
+ if var_def_id( cx, base_left ) != var_def_id( cx, base_right ) ;
1026
1027
then {
1027
- Some ( ( FixedOffsetVar { var : seqexpr_left , offset : offset_left } ,
1028
- FixedOffsetVar { var : seqexpr_right , offset : offset_right } ) )
1028
+ Some ( ( IndexExpr { base : base_left , idx_offset : offset_left } ,
1029
+ IndexExpr { base : base_right , idx_offset : offset_right } ) )
1029
1030
} else {
1030
1031
None
1031
1032
}
0 commit comments