From 2eea72a9360361919f28199db0c6c805e552df2c Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sun, 16 Jun 2024 21:31:55 +0200 Subject: [PATCH] Simplify things (follow review comments) --- clippy_lints/src/redundant_closure_call.rs | 17 +++---------- clippy_lints/src/unconditional_recursion.rs | 3 --- clippy_lints/src/unused_peekable.rs | 28 +++++++++------------ 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/clippy_lints/src/redundant_closure_call.rs b/clippy_lints/src/redundant_closure_call.rs index 4ab707643d02..bad9b979203f 100644 --- a/clippy_lints/src/redundant_closure_call.rs +++ b/clippy_lints/src/redundant_closure_call.rs @@ -43,22 +43,12 @@ declare_clippy_lint! { declare_lint_pass!(RedundantClosureCall => [REDUNDANT_CLOSURE_CALL]); // Used to find `return` statements or equivalents e.g., `?` -struct ReturnVisitor { - found_return: bool, -} - -impl ReturnVisitor { - #[must_use] - fn new() -> Self { - Self { found_return: false } - } -} +struct ReturnVisitor; impl<'tcx> Visitor<'tcx> for ReturnVisitor { type Result = ControlFlow<()>; fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> ControlFlow<()> { if let ExprKind::Ret(_) | ExprKind::Match(.., hir::MatchSource::TryDesugar(_)) = ex.kind { - self.found_return = true; return ControlFlow::Break(()); } hir_visit::walk_expr(self, ex) @@ -103,9 +93,8 @@ fn find_innermost_closure<'tcx>( while let ExprKind::Closure(closure) = expr.kind && let body = cx.tcx.hir().body(closure.body) && { - let mut visitor = ReturnVisitor::new(); - visitor.visit_expr(body.value); - !visitor.found_return + let mut visitor = ReturnVisitor; + !visitor.visit_expr(body.value).is_break() } && steps > 0 { diff --git a/clippy_lints/src/unconditional_recursion.rs b/clippy_lints/src/unconditional_recursion.rs index 80a029da1c1c..14df94a651ac 100644 --- a/clippy_lints/src/unconditional_recursion.rs +++ b/clippy_lints/src/unconditional_recursion.rs @@ -277,7 +277,6 @@ struct CheckCalls<'a, 'tcx> { cx: &'a LateContext<'tcx>, map: Map<'tcx>, implemented_ty_id: DefId, - found_default_call: bool, method_span: Span, } @@ -302,7 +301,6 @@ where && let Some(trait_def_id) = self.cx.tcx.trait_of_item(method_def_id) && self.cx.tcx.is_diagnostic_item(sym::Default, trait_def_id) { - self.found_default_call = true; span_error(self.cx, self.method_span, expr); return ControlFlow::Break(()); } @@ -384,7 +382,6 @@ impl UnconditionalRecursion { cx, map: cx.tcx.hir(), implemented_ty_id, - found_default_call: false, method_span, }; walk_body(&mut c, body); diff --git a/clippy_lints/src/unused_peekable.rs b/clippy_lints/src/unused_peekable.rs index 3dd90aabcf7c..f6a75da01971 100644 --- a/clippy_lints/src/unused_peekable.rs +++ b/clippy_lints/src/unused_peekable.rs @@ -71,15 +71,22 @@ impl<'tcx> LateLintPass<'tcx> for UnusedPeekable { return; } + let mut found_peek_call = false; + for stmt in &block.stmts[idx..] { - vis.visit_stmt(stmt); + if vis.visit_stmt(stmt).is_break() { + found_peek_call = true; + break; + } } - if let Some(expr) = block.expr { - vis.visit_expr(expr); + if !found_peek_call && let Some(expr) = block.expr { + if vis.visit_expr(expr).is_break() { + found_peek_call = true + } } - if !vis.found_peek_call { + if !found_peek_call { span_lint_hir_and_then( cx, UNUSED_PEEKABLE, @@ -99,16 +106,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedPeekable { struct PeekableVisitor<'a, 'tcx> { cx: &'a LateContext<'tcx>, expected_hir_id: HirId, - found_peek_call: bool, } impl<'a, 'tcx> PeekableVisitor<'a, 'tcx> { fn new(cx: &'a LateContext<'tcx>, expected_hir_id: HirId) -> Self { - Self { - cx, - expected_hir_id, - found_peek_call: false, - } + Self { cx, expected_hir_id } } } @@ -139,7 +141,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> { } if args.iter().any(|arg| arg_is_mut_peekable(self.cx, arg)) { - self.found_peek_call = true; return ControlFlow::Break(()); } @@ -161,7 +162,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> { if matches!(method_name, "peek" | "peek_mut" | "next_if" | "next_if_eq") && arg_is_mut_peekable(self.cx, self_arg) { - self.found_peek_call = true; return ControlFlow::Break(()); } @@ -169,7 +169,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> { if remaining_args.iter().any(|arg| arg_is_mut_peekable(self.cx, arg)) && !is_trait_method(self.cx, expr, sym::Iterator) { - self.found_peek_call = true; return ControlFlow::Break(()); } @@ -184,14 +183,12 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> { }, ExprKind::AddrOf(_, Mutability::Not, _) => return ControlFlow::Continue(()), _ => { - self.found_peek_call = true; return ControlFlow::Break(()); }, } }, Node::LetStmt(LetStmt { init: Some(init), .. }) => { if arg_is_mut_peekable(self.cx, init) { - self.found_peek_call = true; return ControlFlow::Break(()); } @@ -200,7 +197,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> { Node::Stmt(stmt) => { match stmt.kind { StmtKind::Let(_) | StmtKind::Item(_) => { - self.found_peek_call = true; return ControlFlow::Break(()); }, StmtKind::Expr(_) | StmtKind::Semi(_) => {},