Skip to content

Commit 752d1be

Browse files
Urgauxobs
authored andcommitted
Fix dropping_copy_types lint from linting in match-arm with side-effects
1 parent 162e5eb commit 752d1be

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

compiler/rustc_lint/src/drop_forget_useless.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn is_single_call_in_arm<'tcx>(
154154
arg: &'tcx Expr<'_>,
155155
drop_expr: &'tcx Expr<'_>,
156156
) -> bool {
157-
if matches!(arg.kind, ExprKind::Call(..) | ExprKind::MethodCall(..)) {
157+
if arg.can_have_side_effects() {
158158
let parent_node = cx.tcx.hir().find_parent(drop_expr.hir_id);
159159
if let Some(Node::Arm(Arm { body, .. })) = &parent_node {
160160
return body.hir_id == drop_expr.hir_id;

tests/ui/lint/dropping_copy_types.rs

+19
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,22 @@ fn issue9482(x: u8) {
7777
_ => (),
7878
}
7979
}
80+
81+
fn issue112653() {
82+
fn foo() -> Result<u8, ()> {
83+
println!("doing foo");
84+
Ok(0) // result is not always useful, the side-effect matters
85+
}
86+
fn bar() {
87+
println!("doing bar");
88+
}
89+
90+
fn stuff() -> Result<(), ()> {
91+
match 42 {
92+
0 => drop(foo()?), // drop is needed because we only care about side-effects
93+
1 => bar(),
94+
_ => (), // doing nothing (no side-effects needed here)
95+
}
96+
Ok(())
97+
}
98+
}

tests/ui/lint/dropping_references.rs

+19
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,22 @@ fn issue10122(x: u8) {
9797
_ => (),
9898
}
9999
}
100+
101+
fn issue112653() {
102+
fn foo() -> Result<&'static u8, ()> {
103+
println!("doing foo");
104+
Ok(&0) // result is not always useful, the side-effect matters
105+
}
106+
fn bar() {
107+
println!("doing bar");
108+
}
109+
110+
fn stuff() -> Result<(), ()> {
111+
match 42 {
112+
0 => drop(foo()?), // drop is needed because we only care about side-effects
113+
1 => bar(),
114+
_ => (), // doing nothing (no side-effects needed here)
115+
}
116+
Ok(())
117+
}
118+
}

0 commit comments

Comments
 (0)