Skip to content

Commit e3ccab4

Browse files
committed
remove last statement special casing
1 parent a089c00 commit e3ccab4

5 files changed

+16
-47
lines changed

clippy_lints/src/zombie_processes.rs

+6-34
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,18 @@ impl<'tcx> LateLintPass<'tcx> for ZombieProcesses {
6060
}
6161

6262
// Don't emit a suggestion since the binding is used later
63-
check(cx, expr, local.hir_id, false);
63+
check(cx, expr, false);
6464
},
65-
Node::LetStmt(&LetStmt { pat, hir_id, .. }) if let PatKind::Wild = pat.kind => {
65+
Node::LetStmt(&LetStmt { pat, .. }) if let PatKind::Wild = pat.kind => {
6666
// `let _ = child;`, also dropped immediately without `wait()`ing
67-
check(cx, expr, hir_id, true);
67+
check(cx, expr, true);
6868
},
6969
Node::Stmt(&Stmt {
7070
kind: StmtKind::Semi(_),
71-
hir_id,
7271
..
7372
}) => {
7473
// Immediately dropped. E.g. `std::process::Command::new("echo").spawn().unwrap();`
75-
check(cx, expr, hir_id, true);
74+
check(cx, expr, true);
7675
},
7776
_ => {},
7877
}
@@ -212,9 +211,8 @@ impl<'a, 'tcx> Visitor<'tcx> for WaitFinder<'a, 'tcx> {
212211
/// such as checking that the binding is not used in certain ways, which isn't necessary for
213212
/// `let _ = <expr that spawns child>;`.
214213
///
215-
/// This checks if the program doesn't unconditionally exit after the spawn expression and that it
216-
/// isn't the last statement of the program.
217-
fn check<'tcx>(cx: &LateContext<'tcx>, spawn_expr: &'tcx Expr<'tcx>, node_id: HirId, emit_suggestion: bool) {
214+
/// This checks if the program doesn't unconditionally exit after the spawn expression.
215+
fn check<'tcx>(cx: &LateContext<'tcx>, spawn_expr: &'tcx Expr<'tcx>, emit_suggestion: bool) {
218216
let Some(block) = get_enclosing_block(cx, spawn_expr.hir_id) else {
219217
return;
220218
};
@@ -228,12 +226,6 @@ fn check<'tcx>(cx: &LateContext<'tcx>, spawn_expr: &'tcx Expr<'tcx>, node_id: Hi
228226
return;
229227
}
230228

231-
// This might be the last effective node of the program (main function).
232-
// There's no need to lint in that case either, as this is basically equivalent to calling `exit()`
233-
if is_last_node_in_main(cx, node_id) {
234-
return;
235-
}
236-
237229
span_lint_and_then(
238230
cx,
239231
ZOMBIE_PROCESSES,
@@ -257,26 +249,6 @@ fn check<'tcx>(cx: &LateContext<'tcx>, spawn_expr: &'tcx Expr<'tcx>, node_id: Hi
257249
);
258250
}
259251

260-
/// The hir id id may either correspond to a `Local` or `Stmt`, depending on how we got here.
261-
/// This function gets the enclosing function, checks if it's `main` and if so,
262-
/// check if the last statement modulo blocks is the given id.
263-
fn is_last_node_in_main(cx: &LateContext<'_>, id: HirId) -> bool {
264-
let hir = cx.tcx.hir();
265-
let body_owner = hir.enclosing_body_owner(id);
266-
let enclosing_body = hir.body_owned_by(body_owner);
267-
268-
if let Some((main_def_id, _)) = cx.tcx.entry_fn(())
269-
&& main_def_id == body_owner.to_def_id()
270-
&& let ExprKind::Block(block, _) = &enclosing_body.value.peel_blocks().kind
271-
&& let [.., stmt] = block.stmts
272-
{
273-
matches!(stmt.kind, StmtKind::Let(local) if local.hir_id == id)
274-
|| matches!(stmt.kind, StmtKind::Semi(..) if stmt.hir_id == id)
275-
} else {
276-
false
277-
}
278-
}
279-
280252
/// Checks if the given expression exits the process.
281253
fn is_exit_expression(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
282254
fn_def_id(cx, expr).is_some_and(|fn_did| {

tests/ui/zombie_processes.rs

-9
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,6 @@ fn main() {
131131
}
132132
x.wait().unwrap();
133133
}
134-
135-
// Checking that it won't lint if spawn is the last statement of a main function.
136-
// IMPORTANT: this case must always be at the very end so it always tests the right thing.
137-
// Don't move this.
138-
{
139-
{
140-
Command::new("").spawn().unwrap();
141-
}
142-
}
143134
}
144135

145136
fn process_child(c: Child) {

tests/ui/zombie_processes_fixable.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::zombie_processes)]
2+
#![allow(clippy::needless_return)]
23

34
use std::process::{Child, Command};
45

@@ -19,3 +20,7 @@ fn not_main() {
1920
fn spawn_proc() -> Child {
2021
Command::new("").spawn().unwrap()
2122
}
23+
24+
fn spawn_proc_2() -> Child {
25+
return Command::new("").spawn().unwrap();
26+
}

tests/ui/zombie_processes_fixable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::zombie_processes)]
2+
#![allow(clippy::needless_return)]
23

34
use std::process::{Child, Command};
45

tests/ui/zombie_processes_fixable.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: spawned process is never `wait()`ed on
2-
--> tests/ui/zombie_processes_fixable.rs:6:13
2+
--> tests/ui/zombie_processes_fixable.rs:7:13
33
|
44
LL | let _ = Command::new("").spawn().unwrap();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try: `.wait()`
@@ -10,7 +10,7 @@ LL | let _ = Command::new("").spawn().unwrap();
1010
= help: to override `-D warnings` add `#[allow(clippy::zombie_processes)]`
1111

1212
error: spawned process is never `wait()`ed on
13-
--> tests/ui/zombie_processes_fixable.rs:8:5
13+
--> tests/ui/zombie_processes_fixable.rs:9:5
1414
|
1515
LL | Command::new("").spawn().unwrap();
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try: `.wait()`
@@ -19,7 +19,7 @@ LL | Command::new("").spawn().unwrap();
1919
= note: see https://doc.rust-lang.org/stable/std/process/struct.Child.html#warning
2020

2121
error: spawned process is never `wait()`ed on
22-
--> tests/ui/zombie_processes_fixable.rs:10:5
22+
--> tests/ui/zombie_processes_fixable.rs:11:5
2323
|
2424
LL | spawn_proc();
2525
| ^^^^^^^^^^^^- help: try: `.wait()`
@@ -28,7 +28,7 @@ LL | spawn_proc();
2828
= note: see https://doc.rust-lang.org/stable/std/process/struct.Child.html#warning
2929

3030
error: spawned process is never `wait()`ed on
31-
--> tests/ui/zombie_processes_fixable.rs:16:5
31+
--> tests/ui/zombie_processes_fixable.rs:17:5
3232
|
3333
LL | Command::new("").spawn().unwrap();
3434
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try: `.wait()`

0 commit comments

Comments
 (0)