Skip to content

zombie_processes: do not complain about early early returns #14912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions clippy_lints/src/zombie_processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_ast::Mutability;
use rustc_ast::visit::visit_opt;
use rustc_errors::Applicability;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{Visitor, walk_block, walk_expr, walk_local};
use rustc_hir::intravisit::{Visitor, walk_block, walk_expr};
use rustc_hir::{Expr, ExprKind, HirId, LetStmt, Node, PatKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter;
Expand Down Expand Up @@ -69,8 +69,9 @@ impl<'tcx> LateLintPass<'tcx> for ZombieProcesses {
let mut vis = WaitFinder {
cx,
local_id,
create_id: expr.hir_id,
body_id: cx.tcx.hir_enclosing_body_owner(expr.hir_id),
state: VisitorState::WalkUpToLocal,
state: VisitorState::WalkUpToCreate,
early_return: None,
missing_wait_branch: None,
};
Expand Down Expand Up @@ -131,6 +132,7 @@ struct MaybeWait(Span);
struct WaitFinder<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
local_id: HirId,
create_id: HirId,
body_id: LocalDefId,
state: VisitorState,
early_return: Option<Span>,
Expand All @@ -141,8 +143,8 @@ struct WaitFinder<'a, 'tcx> {

#[derive(PartialEq)]
enum VisitorState {
WalkUpToLocal,
LocalFound,
WalkUpToCreate,
CreateFound,
}

#[derive(Copy, Clone)]
Expand All @@ -155,19 +157,13 @@ impl<'tcx> Visitor<'tcx> for WaitFinder<'_, 'tcx> {
type NestedFilter = nested_filter::OnlyBodies;
type Result = ControlFlow<MaybeWait>;

fn visit_local(&mut self, l: &'tcx LetStmt<'tcx>) -> Self::Result {
if self.state == VisitorState::WalkUpToLocal
&& let PatKind::Binding(_, pat_id, ..) = l.pat.kind
&& self.local_id == pat_id
{
self.state = VisitorState::LocalFound;
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) -> Self::Result {
if ex.hir_id == self.create_id {
self.state = VisitorState::CreateFound;
return Continue(());
}

walk_local(self, l)
}

fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) -> Self::Result {
if self.state != VisitorState::LocalFound {
if self.state != VisitorState::CreateFound {
return walk_expr(self, ex);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/zombie_processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,13 @@ mod issue14677 {
child.wait().unwrap();
}
}

fn issue14911() -> std::io::Result<String> {
let (mut recv, send) = std::io::pipe()?;
let mut command = Command::new("ls")
.stdout(send.try_clone()?)
.spawn()
.expect("Could not spawn new process...");
command.wait()?;
Ok("".into())
}
Loading