Skip to content

Commit e855fe3

Browse files
committed
Reflect the changes that has been made and fmt
1 parent ce653d6 commit e855fe3

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

clippy_lints/src/loops.rs

+18-27
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ impl<'a> MinifyingSugg<'a> {
846846
s.as_ref()
847847
}
848848

849-
fn hir(cx: &LateContext<'_, '_>, expr: &Expr<'_>, default: &'a str) -> Self {
849+
fn hir(cx: &LateContext<'_>, expr: &Expr<'_>, default: &'a str) -> Self {
850850
Self(sugg::Sugg::hir(cx, expr, default))
851851
}
852852

@@ -947,11 +947,7 @@ fn get_details_from_idx<'tcx>(
947947
idx: &Expr<'_>,
948948
starts: &[Start<'tcx>],
949949
) -> Option<(StartKind<'tcx>, Offset)> {
950-
fn get_start<'tcx>(
951-
cx: &LateContext<'tcx>,
952-
e: &Expr<'_>,
953-
starts: &[Start<'tcx>],
954-
) -> Option<StartKind<'tcx>> {
950+
fn get_start<'tcx>(cx: &LateContext<'tcx>, e: &Expr<'_>, starts: &[Start<'tcx>]) -> Option<StartKind<'tcx>> {
955951
starts.iter().find_map(|start| {
956952
if same_var(cx, e, start.id) {
957953
Some(start.kind)
@@ -982,13 +978,9 @@ fn get_details_from_idx<'tcx>(
982978
match idx.kind {
983979
ExprKind::Binary(op, lhs, rhs) => match op.node {
984980
BinOpKind::Add => {
985-
let offset_opt = if let Some(s) = get_start(cx, lhs, starts) {
986-
get_offset(cx, rhs, starts).map(|o| (s, o))
987-
} else if let Some(s) = get_start(cx, rhs, starts) {
988-
get_offset(cx, lhs, starts).map(|o| (s, o))
989-
} else {
990-
None
991-
};
981+
let offset_opt = get_start(cx, lhs, starts)
982+
.and_then(|s| get_offset(cx, rhs, starts).map(|o| (s, o)))
983+
.or_else(|| get_start(cx, rhs, starts).and_then(|s| get_offset(cx, lhs, starts).map(|o| (s, o))));
992984

993985
offset_opt.map(|(s, o)| (s, Offset::positive(o)))
994986
},
@@ -1011,7 +1003,7 @@ fn get_assignment<'tcx>(e: &'tcx Expr<'tcx>) -> Option<(&'tcx Expr<'tcx>, &'tcx
10111003
}
10121004

10131005
fn get_assignments<'a: 'c, 'tcx: 'c, 'c>(
1014-
cx: &'a LateContext<'a, 'tcx>,
1006+
cx: &'a LateContext<'tcx>,
10151007
stmts: &'tcx [Stmt<'tcx>],
10161008
expr: Option<&'tcx Expr<'tcx>>,
10171009
loop_counters: &'c [Start<'tcx>],
@@ -1032,7 +1024,7 @@ fn get_assignments<'a: 'c, 'tcx: 'c, 'c>(
10321024
}
10331025

10341026
fn get_loop_counters<'a, 'tcx>(
1035-
cx: &'a LateContext<'a, 'tcx>,
1027+
cx: &'a LateContext<'tcx>,
10361028
body: &'tcx Block<'tcx>,
10371029
expr: &'tcx Expr<'_>,
10381030
) -> Option<impl Iterator<Item = Start<'tcx>> + 'a> {
@@ -1042,7 +1034,7 @@ fn get_loop_counters<'a, 'tcx>(
10421034

10431035
// For each candidate, check the parent block to see if
10441036
// it's initialized to zero at the start of the loop.
1045-
if let Some(block) = get_enclosing_block(&cx, expr.hir_id) {
1037+
get_enclosing_block(&cx, expr.hir_id).and_then(|block| {
10461038
increment_visitor
10471039
.into_results()
10481040
.filter_map(move |var_id| {
@@ -1055,9 +1047,7 @@ fn get_loop_counters<'a, 'tcx>(
10551047
})
10561048
})
10571049
.into()
1058-
} else {
1059-
None
1060-
}
1050+
})
10611051
}
10621052

10631053
fn build_manual_memcpy_suggestion<'tcx>(
@@ -2315,7 +2305,7 @@ struct IncrementVisitor<'a, 'tcx> {
23152305
}
23162306

23172307
impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
2318-
fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
2308+
fn new(cx: &'a LateContext<'tcx>) -> Self {
23192309
Self {
23202310
cx,
23212311
states: FxHashMap::default(),
@@ -2396,7 +2386,10 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
23962386
enum InitializeVisitorState<'hir> {
23972387
Initial, // Not examined yet
23982388
Declared(Symbol), // Declared but not (yet) initialized
2399-
Initialized { name: Symbol, initializer: &'hir Expr<'hir> },
2389+
Initialized {
2390+
name: Symbol,
2391+
initializer: &'hir Expr<'hir>,
2392+
},
24002393
DontWarn,
24012394
}
24022395

@@ -2412,7 +2405,7 @@ struct InitializeVisitor<'a, 'tcx> {
24122405
}
24132406

24142407
impl<'a, 'tcx> InitializeVisitor<'a, 'tcx> {
2415-
fn new(cx: &'a LateContext<'a, 'tcx>, end_expr: &'tcx Expr<'tcx>, var_id: HirId) -> Self {
2408+
fn new(cx: &'a LateContext<'tcx>, end_expr: &'tcx Expr<'tcx>, var_id: HirId) -> Self {
24162409
Self {
24172410
cx,
24182411
end_expr,
@@ -2423,7 +2416,7 @@ impl<'a, 'tcx> InitializeVisitor<'a, 'tcx> {
24232416
}
24242417
}
24252418

2426-
fn get_result(&self) -> Option<(Name, &'tcx Expr<'tcx>)> {
2419+
fn get_result(&self) -> Option<(Symbol, &'tcx Expr<'tcx>)> {
24272420
if let InitializeVisitorState::Initialized { name, initializer } = self.state {
24282421
Some((name, initializer))
24292422
} else {
@@ -2442,14 +2435,12 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
24422435
if local.pat.hir_id == self.var_id;
24432436
if let PatKind::Binding(.., ident, _) = local.pat.kind;
24442437
then {
2445-
self.state = if let Some(ref init) = local.init {
2438+
self.state = local.init.map_or(InitializeVisitorState::Declared(ident.name), |init| {
24462439
InitializeVisitorState::Initialized {
24472440
initializer: init,
24482441
name: ident.name,
24492442
}
2450-
} else {
2451-
InitializeVisitorState::Declared(ident.name)
2452-
}
2443+
})
24532444
}
24542445
}
24552446
walk_stmt(self, stmt);

0 commit comments

Comments
 (0)