Skip to content

Commit

Permalink
Simplify things (follow review comments)
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Jun 16, 2024
1 parent d241556 commit 2eea72a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
17 changes: 3 additions & 14 deletions clippy_lints/src/redundant_closure_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
{
Expand Down
3 changes: 0 additions & 3 deletions clippy_lints/src/unconditional_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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(());
}
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 12 additions & 16 deletions clippy_lints/src/unused_peekable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }
}
}

Expand Down Expand Up @@ -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(());
}

Expand All @@ -161,15 +162,13 @@ 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(());
}

// foo.some_method() excluding Iterator methods
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(());
}

Expand All @@ -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(());
}

Expand All @@ -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(_) => {},
Expand Down

0 comments on commit 2eea72a

Please sign in to comment.