Skip to content

Commit 4f2617c

Browse files
committed
Separate getting offsets and getting index expressions
1 parent 3d121d5 commit 4f2617c

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

clippy_lints/src/loops.rs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ fn fetch_cloned_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
837837
}
838838
}
839839

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> {
841841
fn extract_offset<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, e: &Expr<'_>, var: HirId) -> Option<String> {
842842
match &e.kind {
843843
ExprKind::Lit(l) => match l.node {
@@ -849,38 +849,24 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, v
849849
}
850850
}
851851

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+
};
868862

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)
873864
},
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),
875866
_ => 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,
884870
}
885871
}
886872

@@ -994,15 +980,22 @@ fn detect_manual_memcpy<'a, 'tcx>(
994980
o.and_then(|(lhs, rhs)| {
995981
let rhs = fetch_cloned_expr(rhs);
996982
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());
999991

1000992
// Source and destination must be different
1001-
if offset_left.var_name != offset_right.var_name;
993+
if var_name_left != var_name_right;
1002994
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 }))
1004997
} else {
1005-
return None
998+
None
1006999
}
10071000
}
10081001
})

0 commit comments

Comments
 (0)