Skip to content

Commit 7dd0f34

Browse files
committed
Refactor if to use else and iterator combinators
1 parent c94f0f4 commit 7dd0f34

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

clippy_lints/src/loops.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,11 @@ fn same_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, var: HirId) -
779779
// our variable!
780780
if local_id == var;
781781
then {
782-
return true;
782+
true
783+
} else {
784+
false
783785
}
784786
}
785-
786-
false
787787
}
788788

789789
struct Offset {
@@ -853,13 +853,7 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, v
853853
BinOpKind::Sub if same_var(cx, lhs, var) => extract_offset(cx, rhs, var).map(Offset::negative),
854854
_ => None,
855855
},
856-
ExprKind::Path(..) => {
857-
if same_var(cx, idx, var) {
858-
Some(Offset::positive("0".into()))
859-
} else {
860-
None
861-
}
862-
},
856+
ExprKind::Path(..) if same_var(cx, idx, var) => Some(Offset::positive("0".into())),
863857
_ => None,
864858
};
865859

@@ -883,11 +877,11 @@ fn fetch_cloned_fixed_offset_var<'a, 'tcx>(
883877
if args.len() == 1;
884878
if let Some(arg) = args.get(0);
885879
then {
886-
return get_fixed_offset_var(cx, arg, var);
880+
get_fixed_offset_var(cx, arg, var)
881+
} else {
882+
get_fixed_offset_var(cx, expr, var)
887883
}
888884
}
889-
890-
get_fixed_offset_var(cx, expr, var)
891885
}
892886

893887
fn get_indexed_assignments<'a, 'tcx>(
@@ -925,12 +919,12 @@ fn get_indexed_assignments<'a, 'tcx>(
925919

926920
stmts
927921
.iter()
928-
.map(|stmt| match stmt.kind {
922+
.filter_map(|stmt| match stmt.kind {
929923
StmtKind::Local(..) | StmtKind::Item(..) => None,
930-
StmtKind::Expr(e) | StmtKind::Semi(e) => Some(get_assignment(cx, e, var)),
924+
StmtKind::Expr(e) | StmtKind::Semi(e) => Some(e),
931925
})
932-
.chain(expr.as_ref().into_iter().map(|e| Some(get_assignment(cx, &*e, var))))
933-
.filter_map(|op| op)
926+
.chain(expr.into_iter())
927+
.map(|op| get_assignment(cx, op, var))
934928
.collect::<Option<Vec<_>>>()
935929
.unwrap_or_default()
936930
} else {
@@ -996,23 +990,23 @@ fn detect_manual_memcpy<'a, 'tcx>(
996990
if let Some(arg) = len_args.get(0);
997991
if snippet(cx, arg.span, "??") == var_name;
998992
then {
999-
return if offset.negate {
993+
if offset.negate {
1000994
format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value)
1001995
} else {
1002996
String::new()
997+
}
998+
} else {
999+
let end_str = match limits {
1000+
ast::RangeLimits::Closed => {
1001+
let end = sugg::Sugg::hir(cx, end, "<count>");
1002+
format!("{}", end + sugg::ONE)
1003+
},
1004+
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
10031005
};
1006+
1007+
print_sum(&Offset::positive(end_str), &offset)
10041008
}
10051009
}
1006-
1007-
let end_str = match limits {
1008-
ast::RangeLimits::Closed => {
1009-
let end = sugg::Sugg::hir(cx, end, "<count>");
1010-
format!("{}", end + sugg::ONE)
1011-
},
1012-
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
1013-
};
1014-
1015-
print_sum(&Offset::positive(end_str), &offset)
10161010
};
10171011

10181012
// The only statements in the for loops can be indexed assignments from

0 commit comments

Comments
 (0)