diff options
| author | Urgau <urgau@numericable.fr> | 2023-07-01 16:05:55 +0200 |
|---|---|---|
| committer | Urgau <urgau@numericable.fr> | 2023-07-01 16:05:55 +0200 |
| commit | 908574b5e761fca6e0b03ef4e2c8640fcff1218a (patch) | |
| tree | 3210cd0f0604fc24c713bfcc0718974e9c55e667 | |
| parent | e69c7306e2be08939d95f14229e3f96566fb206c (diff) | |
| download | rust-908574b5e761fca6e0b03ef4e2c8640fcff1218a.tar.gz rust-908574b5e761fca6e0b03ef4e2c8640fcff1218a.zip | |
Fix dropping_copy_types lint from linting in match-arm with side-effects
| -rw-r--r-- | compiler/rustc_lint/src/drop_forget_useless.rs | 2 | ||||
| -rw-r--r-- | tests/ui/lint/dropping_copy_types.rs | 19 | ||||
| -rw-r--r-- | tests/ui/lint/dropping_references.rs | 19 |
3 files changed, 39 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/drop_forget_useless.rs b/compiler/rustc_lint/src/drop_forget_useless.rs index 4cea6169dc3..467f53d445c 100644 --- a/compiler/rustc_lint/src/drop_forget_useless.rs +++ b/compiler/rustc_lint/src/drop_forget_useless.rs @@ -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; diff --git a/tests/ui/lint/dropping_copy_types.rs b/tests/ui/lint/dropping_copy_types.rs index 2937320e5d8..2412222d6d1 100644 --- a/tests/ui/lint/dropping_copy_types.rs +++ b/tests/ui/lint/dropping_copy_types.rs @@ -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(()) + } +} diff --git a/tests/ui/lint/dropping_references.rs b/tests/ui/lint/dropping_references.rs index 0d5d484f451..bb02cb75a90 100644 --- a/tests/ui/lint/dropping_references.rs +++ b/tests/ui/lint/dropping_references.rs @@ -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(()) + } +} |
