Skip to content

Commit c52b389

Browse files
committed
Fix readability suggestions.
1. Make the lifetime contained in LateContext `'tcx`. 2. Fix `'txc` to `'tcx` because it was a typo. 3. Refactor `IterFunctionVisitor`'s `visit_block` method to be more readable. 4. Replace uses of `rustc_middle::ty::TyKind` with `rustc::middle::ty`, and remove the `#[allow(...)]`. (Thank you llogiq for all these suggestions!)
1 parent 5b3c00f commit c52b389

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

clippy_lints/src/loops/needless_collect.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::{Block, Expr, ExprKind, HirId, HirIdSet, Local, Mutability, Node,
1212
use rustc_lint::LateContext;
1313
use rustc_middle::hir::map::Map;
1414
use rustc_middle::ty::subst::GenericArgKind;
15-
use rustc_middle::ty::{TyKind, TyS};
15+
use rustc_middle::ty::{self, TyS};
1616
use rustc_span::sym;
1717
use rustc_span::{MultiSpan, Span};
1818

@@ -171,27 +171,23 @@ enum IterFunctionKind {
171171
Contains(Span),
172172
}
173173

174-
struct IterFunctionVisitor<'b, 'a> {
174+
struct IterFunctionVisitor<'a, 'tcx> {
175175
illegal_mutable_capture_ids: HirIdSet,
176176
current_mutably_captured_ids: HirIdSet,
177-
cx: &'a LateContext<'b>,
177+
cx: &'a LateContext<'tcx>,
178178
uses: Vec<Option<IterFunction>>,
179179
hir_id_uses_map: FxHashMap<HirId, usize>,
180180
current_statement_hir_id: Option<HirId>,
181181
seen_other: bool,
182182
target: HirId,
183183
}
184184
impl<'tcx> Visitor<'tcx> for IterFunctionVisitor<'_, 'tcx> {
185-
fn visit_block(&mut self, block: &'txc Block<'tcx>) {
186-
for (expr, hir_id) in block
187-
.stmts
188-
.iter()
189-
.filter_map(get_expr_and_hir_id_from_stmt)
190-
.chain(block.expr.map(|expr| (expr, None)))
191-
{
192-
self.current_statement_hir_id = hir_id;
193-
self.current_mutably_captured_ids = get_captured_ids(self.cx, self.cx.typeck_results().expr_ty(expr));
194-
self.visit_expr(expr);
185+
fn visit_block(&mut self, block: &'tcx Block<'tcx>) {
186+
for (expr, hir_id) in block.stmts.iter().filter_map(get_expr_and_hir_id_from_stmt) {
187+
self.visit_block_expr(expr, hir_id);
188+
}
189+
if let Some(expr) = block.expr {
190+
self.visit_block_expr(expr, None);
195191
}
196192
}
197193

@@ -273,6 +269,14 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor<'_, 'tcx> {
273269
}
274270
}
275271

272+
impl<'tcx> IterFunctionVisitor<'_, 'tcx> {
273+
fn visit_block_expr(&mut self, expr: &'tcx Expr<'tcx>, hir_id: Option<HirId>) {
274+
self.current_statement_hir_id = hir_id;
275+
self.current_mutably_captured_ids = get_captured_ids(self.cx, self.cx.typeck_results().expr_ty(expr));
276+
self.visit_expr(expr);
277+
}
278+
}
279+
276280
fn get_expr_and_hir_id_from_stmt<'v>(stmt: &'v Stmt<'v>) -> Option<(&'v Expr<'v>, Option<HirId>)> {
277281
match stmt.kind {
278282
StmtKind::Expr(expr) | StmtKind::Semi(expr) => Some((expr, None)),
@@ -335,18 +339,17 @@ fn detect_iter_and_into_iters<'tcx: 'a, 'a>(
335339
}
336340
}
337341

338-
#[allow(rustc::usage_of_ty_tykind)]
339342
fn get_captured_ids(cx: &LateContext<'tcx>, ty: &'_ TyS<'_>) -> HirIdSet {
340343
fn get_captured_ids_recursive(cx: &LateContext<'tcx>, ty: &'_ TyS<'_>, set: &mut HirIdSet) {
341344
match ty.kind() {
342-
TyKind::Adt(_, generics) => {
345+
ty::Adt(_, generics) => {
343346
for generic in *generics {
344347
if let GenericArgKind::Type(ty) = generic.unpack() {
345348
get_captured_ids_recursive(cx, ty, set);
346349
}
347350
}
348351
},
349-
TyKind::Closure(def_id, _) => {
352+
ty::Closure(def_id, _) => {
350353
let closure_hir_node = cx.tcx.hir().get_if_local(*def_id).unwrap();
351354
if let Node::Expr(closure_expr) = closure_hir_node {
352355
can_move_expr_to_closure(cx, closure_expr)

0 commit comments

Comments
 (0)