Skip to content

Commit

Permalink
Fix dropping_copy_types lint from linting in match-arm with side-effects
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jul 1, 2023
1 parent e69c730 commit 908574b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/drop_forget_useless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn is_single_call_in_arm<'tcx>(
arg: &'tcx Expr<'_>,
drop_expr: &'tcx Expr<'_>,
) -> bool {
if matches!(arg.kind, ExprKind::Call(..) | ExprKind::MethodCall(..)) {
if arg.can_have_side_effects() {
let parent_node = cx.tcx.hir().find_parent(drop_expr.hir_id);
if let Some(Node::Arm(Arm { body, .. })) = &parent_node {
return body.hir_id == drop_expr.hir_id;
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/lint/dropping_copy_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ fn issue9482(x: u8) {
_ => (),
}
}

fn issue112653() {
fn foo() -> Result<u8, ()> {
println!("doing foo");
Ok(0) // result is not always useful, the side-effect matters
}
fn bar() {
println!("doing bar");
}

fn stuff() -> Result<(), ()> {
match 42 {
0 => drop(foo()?), // drop is needed because we only care about side-effects
1 => bar(),
_ => (), // doing nothing (no side-effects needed here)
}
Ok(())
}
}
19 changes: 19 additions & 0 deletions tests/ui/lint/dropping_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,22 @@ fn issue10122(x: u8) {
_ => (),
}
}

fn issue112653() {
fn foo() -> Result<&'static u8, ()> {
println!("doing foo");
Ok(&0) // result is not always useful, the side-effect matters
}
fn bar() {
println!("doing bar");
}

fn stuff() -> Result<(), ()> {
match 42 {
0 => drop(foo()?), // drop is needed because we only care about side-effects
1 => bar(),
_ => (), // doing nothing (no side-effects needed here)
}
Ok(())
}
}

0 comments on commit 908574b

Please sign in to comment.