Skip to content

Commit 672fb8e

Browse files
committed
Auto merge of #9491 - kraktus:drop_copy, r=Jarcho
[`drop_copy`]: Do not lint idiomatic in match arm close #9482 changelog: [`drop_copy`]: Do not lint idiomatic in match arm
2 parents 9aa85dc + 187c27e commit 672fb8e

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

clippy_lints/src/drop_forget_ref.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
2+
use clippy_utils::get_parent_node;
23
use clippy_utils::is_must_use_func_call;
34
use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item};
4-
use rustc_hir::{Expr, ExprKind, LangItem};
5+
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node};
56
use rustc_lint::{LateContext, LateLintPass};
67
use rustc_session::{declare_lint_pass, declare_tool_lint};
78
use rustc_span::sym;
@@ -202,11 +203,13 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
202203
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(def_id)
203204
{
204205
let arg_ty = cx.typeck_results().expr_ty(arg);
206+
let is_copy = is_copy(cx, arg_ty);
207+
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
205208
let (lint, msg) = match fn_name {
206209
sym::mem_drop if arg_ty.is_ref() => (DROP_REF, DROP_REF_SUMMARY),
207210
sym::mem_forget if arg_ty.is_ref() => (FORGET_REF, FORGET_REF_SUMMARY),
208-
sym::mem_drop if is_copy(cx, arg_ty) => (DROP_COPY, DROP_COPY_SUMMARY),
209-
sym::mem_forget if is_copy(cx, arg_ty) => (FORGET_COPY, FORGET_COPY_SUMMARY),
211+
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => (DROP_COPY, DROP_COPY_SUMMARY),
212+
sym::mem_forget if is_copy => (FORGET_COPY, FORGET_COPY_SUMMARY),
210213
sym::mem_drop if is_type_lang_item(cx, arg_ty, LangItem::ManuallyDrop) => {
211214
span_lint_and_help(
212215
cx,
@@ -221,7 +224,9 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
221224
sym::mem_drop
222225
if !(arg_ty.needs_drop(cx.tcx, cx.param_env)
223226
|| is_must_use_func_call(cx, arg)
224-
|| is_must_use_ty(cx, arg_ty)) =>
227+
|| is_must_use_ty(cx, arg_ty)
228+
|| drop_is_single_call_in_arm
229+
) =>
225230
{
226231
(DROP_NON_DROP, DROP_NON_DROP_SUMMARY)
227232
},
@@ -241,3 +246,18 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
241246
}
242247
}
243248
}
249+
250+
// dropping returned value of a function like in the following snippet is considered idiomatic, see
251+
// #9482 for examples match <var> {
252+
// <pat> => drop(fn_with_side_effect_and_returning_some_value()),
253+
// ..
254+
// }
255+
fn is_single_call_in_arm<'tcx>(cx: &LateContext<'tcx>, arg: &'tcx Expr<'_>, drop_expr: &'tcx Expr<'_>) -> bool {
256+
if matches!(arg.kind, ExprKind::Call(..) | ExprKind::MethodCall(..)) {
257+
let parent_node = get_parent_node(cx.tcx, drop_expr.hir_id);
258+
if let Some(Node::Arm(Arm { body, .. })) = &parent_node {
259+
return body.hir_id == drop_expr.hir_id;
260+
}
261+
}
262+
false
263+
}

tests/ui/drop_forget_copy.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,23 @@ fn main() {
6464
let a5 = a1.clone();
6565
forget(a5);
6666
}
67+
68+
#[allow(unused)]
69+
#[allow(clippy::unit_cmp)]
70+
fn issue9482(x: u8) {
71+
fn println_and<T>(t: T) -> T {
72+
println!("foo");
73+
t
74+
}
75+
76+
match x {
77+
0 => drop(println_and(12)), // Don't lint (copy type), we only care about side-effects
78+
1 => drop(println_and(String::new())), // Don't lint (no copy type), we only care about side-effects
79+
2 => {
80+
drop(println_and(13)); // Lint, even if we only care about the side-effect, it's already in a block
81+
},
82+
3 if drop(println_and(14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
83+
4 => drop(2), // Lint, not a fn/method call
84+
_ => (),
85+
}
86+
}

tests/ui/drop_forget_copy.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,41 @@ note: argument has type `SomeStruct`
7272
LL | forget(s4);
7373
| ^^
7474

75-
error: aborting due to 6 previous errors
75+
error: calls to `std::mem::drop` with a value that implements `Copy`. Dropping a copy leaves the original intact
76+
--> $DIR/drop_forget_copy.rs:80:13
77+
|
78+
LL | drop(println_and(13)); // Lint, even if we only care about the side-effect, it's already in a block
79+
| ^^^^^^^^^^^^^^^^^^^^^
80+
|
81+
note: argument has type `i32`
82+
--> $DIR/drop_forget_copy.rs:80:18
83+
|
84+
LL | drop(println_and(13)); // Lint, even if we only care about the side-effect, it's already in a block
85+
| ^^^^^^^^^^^^^^^
86+
87+
error: calls to `std::mem::drop` with a value that implements `Copy`. Dropping a copy leaves the original intact
88+
--> $DIR/drop_forget_copy.rs:82:14
89+
|
90+
LL | 3 if drop(println_and(14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
91+
| ^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
note: argument has type `i32`
94+
--> $DIR/drop_forget_copy.rs:82:19
95+
|
96+
LL | 3 if drop(println_and(14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
97+
| ^^^^^^^^^^^^^^^
98+
99+
error: calls to `std::mem::drop` with a value that implements `Copy`. Dropping a copy leaves the original intact
100+
--> $DIR/drop_forget_copy.rs:83:14
101+
|
102+
LL | 4 => drop(2), // Lint, not a fn/method call
103+
| ^^^^^^^
104+
|
105+
note: argument has type `i32`
106+
--> $DIR/drop_forget_copy.rs:83:19
107+
|
108+
LL | 4 => drop(2), // Lint, not a fn/method call
109+
| ^
110+
111+
error: aborting due to 9 previous errors
76112

0 commit comments

Comments
 (0)