Skip to content

Commit 31cb110

Browse files
committed
add concinient methods to Increment/InitializeVisitor
1 parent b2d5b89 commit 31cb110

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

clippy_lints/src/loops.rs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,31 +1536,17 @@ fn check_for_loop_explicit_counter<'tcx>(
15361536
expr: &'tcx Expr<'_>,
15371537
) {
15381538
// Look for variables that are incremented once per loop iteration.
1539-
let mut visitor = IncrementVisitor {
1540-
cx,
1541-
states: FxHashMap::default(),
1542-
depth: 0,
1543-
done: false,
1544-
};
1539+
let mut visitor = IncrementVisitor::new(cx);
15451540
walk_expr(&mut visitor, body);
15461541

15471542
// For each candidate, check the parent block to see if
15481543
// it's initialized to zero at the start of the loop.
15491544
if let Some(block) = get_enclosing_block(&cx, expr.hir_id) {
1550-
for (id, _) in visitor.states.iter().filter(|&(_, v)| *v == VarState::IncrOnce) {
1551-
let mut visitor2 = InitializeVisitor {
1552-
cx,
1553-
end_expr: expr,
1554-
var_id: *id,
1555-
state: VarState::IncrOnce,
1556-
name: None,
1557-
depth: 0,
1558-
past_loop: false,
1559-
};
1545+
for id in visitor.into_results() {
1546+
let mut visitor2 = InitializeVisitor::new(cx, expr, id);
15601547
walk_block(&mut visitor2, block);
15611548

1562-
if visitor2.state == VarState::Warn {
1563-
if let Some(name) = visitor2.name {
1549+
if let Some(name) = visitor2.get_result() {
15641550
let mut applicability = Applicability::MachineApplicable;
15651551

15661552
// for some reason this is the only way to get the `Span`
@@ -1585,7 +1571,6 @@ fn check_for_loop_explicit_counter<'tcx>(
15851571
),
15861572
applicability,
15871573
);
1588-
}
15891574
}
15901575
}
15911576
}
@@ -2142,6 +2127,24 @@ struct IncrementVisitor<'a, 'tcx> {
21422127
done: bool,
21432128
}
21442129

2130+
impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
2131+
fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
2132+
Self {
2133+
cx,
2134+
states: FxHashMap::default(),
2135+
depth: 0,
2136+
done: false,
2137+
}
2138+
}
2139+
2140+
fn into_results(self) -> impl Iterator<Item = HirId> {
2141+
self.states
2142+
.into_iter()
2143+
.filter(|(_, state)| *state == VarState::IncrOnce)
2144+
.map(|(id, _)| id)
2145+
}
2146+
}
2147+
21452148
impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
21462149
type Map = Map<'tcx>;
21472150

@@ -2209,6 +2212,28 @@ struct InitializeVisitor<'a, 'tcx> {
22092212
past_loop: bool,
22102213
}
22112214

2215+
impl<'a, 'tcx> InitializeVisitor<'a, 'tcx> {
2216+
fn new(cx: &'a LateContext<'a, 'tcx>, end_expr: &'tcx Expr<'tcx>, var_id: HirId) -> Self {
2217+
Self {
2218+
cx,
2219+
end_expr,
2220+
var_id,
2221+
state: VarState::IncrOnce,
2222+
name: None,
2223+
depth: 0,
2224+
past_loop: false,
2225+
}
2226+
}
2227+
2228+
fn get_result(&self) -> Option<Name> {
2229+
if self.state == VarState::Warn {
2230+
self.name
2231+
} else {
2232+
None
2233+
}
2234+
}
2235+
}
2236+
22122237
impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
22132238
type Map = Map<'tcx>;
22142239

0 commit comments

Comments
 (0)