@@ -837,7 +837,7 @@ fn fetch_cloned_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
837
837
}
838
838
}
839
839
840
- fn get_fixed_offset_var < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & Expr < ' _ > , var : HirId ) -> Option < FixedOffsetVar > {
840
+ fn get_offset < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , idx : & Expr < ' _ > , var : HirId ) -> Option < Offset > {
841
841
fn extract_offset < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , e : & Expr < ' _ > , var : HirId ) -> Option < String > {
842
842
match & e. kind {
843
843
ExprKind :: Lit ( l) => match l. node {
@@ -849,38 +849,24 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, v
849
849
}
850
850
}
851
851
852
- if let ExprKind :: Index ( seqexpr, idx) = expr. kind {
853
- let ty = cx. tables . expr_ty ( seqexpr) ;
854
- if !is_slice_like ( cx, ty) {
855
- return None ;
856
- }
857
-
858
- let offset = match idx. kind {
859
- ExprKind :: Binary ( op, lhs, rhs) => match op. node {
860
- BinOpKind :: Add => {
861
- let offset_opt = if same_var ( cx, lhs, var) {
862
- extract_offset ( cx, rhs, var)
863
- } else if same_var ( cx, rhs, var) {
864
- extract_offset ( cx, lhs, var)
865
- } else {
866
- None
867
- } ;
852
+ match idx. kind {
853
+ ExprKind :: Binary ( op, lhs, rhs) => match op. node {
854
+ BinOpKind :: Add => {
855
+ let offset_opt = if same_var ( cx, lhs, var) {
856
+ extract_offset ( cx, rhs, var)
857
+ } else if same_var ( cx, rhs, var) {
858
+ extract_offset ( cx, lhs, var)
859
+ } else {
860
+ None
861
+ } ;
868
862
869
- offset_opt. map ( Offset :: positive)
870
- } ,
871
- BinOpKind :: Sub if same_var ( cx, lhs, var) => extract_offset ( cx, rhs, var) . map ( Offset :: negative) ,
872
- _ => None ,
863
+ offset_opt. map ( Offset :: positive)
873
864
} ,
874
- ExprKind :: Path ( .. ) if same_var ( cx, idx , var) => Some ( Offset :: positive ( "0" . into ( ) ) ) ,
865
+ BinOpKind :: Sub if same_var ( cx, lhs , var) => extract_offset ( cx , rhs , var ) . map ( Offset :: negative ) ,
875
866
_ => None ,
876
- } ;
877
-
878
- offset. map ( |o| FixedOffsetVar {
879
- var_name : snippet_opt ( cx, seqexpr. span ) . unwrap_or_else ( || "???" . into ( ) ) ,
880
- offset : o,
881
- } )
882
- } else {
883
- None
867
+ } ,
868
+ ExprKind :: Path ( ..) if same_var ( cx, idx, var) => Some ( Offset :: positive ( "0" . into ( ) ) ) ,
869
+ _ => None ,
884
870
}
885
871
}
886
872
@@ -994,15 +980,22 @@ fn detect_manual_memcpy<'a, 'tcx>(
994
980
o. and_then ( |( lhs, rhs) | {
995
981
let rhs = fetch_cloned_expr ( rhs) ;
996
982
if_chain ! {
997
- if let Some ( offset_left) = get_fixed_offset_var( cx, lhs, canonical_id) ;
998
- if let Some ( offset_right) = get_fixed_offset_var( cx, rhs, canonical_id) ;
983
+ if let ExprKind :: Index ( seqexpr_left, idx_left) = lhs. kind;
984
+ if let ExprKind :: Index ( seqexpr_right, idx_right) = rhs. kind;
985
+ if is_slice_like( cx, cx. tables. expr_ty( seqexpr_left) )
986
+ && is_slice_like( cx, cx. tables. expr_ty( seqexpr_right) ) ;
987
+ if let Some ( offset_left) = get_offset( cx, & idx_left, canonical_id) ;
988
+ if let Some ( offset_right) = get_offset( cx, & idx_right, canonical_id) ;
989
+ let var_name_left = snippet_opt( cx, seqexpr_left. span) . unwrap_or_else( || "???" . into( ) ) ;
990
+ let var_name_right = snippet_opt( cx, seqexpr_right. span) . unwrap_or_else( || "???" . into( ) ) ;
999
991
1000
992
// Source and destination must be different
1001
- if offset_left . var_name != offset_right . var_name ;
993
+ if var_name_left != var_name_right ;
1002
994
then {
1003
- Some ( ( offset_left, offset_right) )
995
+ Some ( ( FixedOffsetVar { var_name: var_name_left, offset: offset_left } ,
996
+ FixedOffsetVar { var_name: var_name_right, offset: offset_right } ) )
1004
997
} else {
1005
- return None
998
+ None
1006
999
}
1007
1000
}
1008
1001
} )
0 commit comments