Skip to content

Commit 01a2a9d

Browse files
committed
[drop_ref]: don't lint idiomatic in match arm
1 parent a85e480 commit 01a2a9d

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

clippy_lints/src/drop_forget_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
206206
let is_copy = is_copy(cx, arg_ty);
207207
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
208208
let (lint, msg) = match fn_name {
209-
sym::mem_drop if arg_ty.is_ref() => (DROP_REF, DROP_REF_SUMMARY),
209+
sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => (DROP_REF, DROP_REF_SUMMARY),
210210
sym::mem_forget if arg_ty.is_ref() => (FORGET_REF, FORGET_REF_SUMMARY),
211211
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => (DROP_COPY, DROP_COPY_SUMMARY),
212212
sym::mem_forget if is_copy => (FORGET_COPY, FORGET_COPY_SUMMARY),

tests/ui/drop_ref.rs

+23
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,26 @@ fn test_owl_result_2() -> Result<u8, ()> {
7272
produce_half_owl_ok().map(drop)?;
7373
Ok(1)
7474
}
75+
76+
#[allow(unused)]
77+
#[allow(clippy::unit_cmp)]
78+
fn issue10122(x: u8) {
79+
// This is a function which returns a reference and has a side-effect, which means
80+
// that calling drop() on the function is considered an idiomatic way of achieving the side-effect
81+
// in a match arm.
82+
fn println_and<T>(t: &T) -> &T {
83+
println!("foo");
84+
t
85+
}
86+
87+
match x {
88+
0 => drop(println_and(&12)), // Don't lint (copy type), we only care about side-effects
89+
1 => drop(println_and(&String::new())), // Don't lint (no copy type), we only care about side-effects
90+
2 => {
91+
drop(println_and(&13)); // Lint, even if we only care about the side-effect, it's already in a block
92+
},
93+
3 if drop(println_and(&14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
94+
4 => drop(&2), // Lint, not a fn/method call
95+
_ => (),
96+
}
97+
}

tests/ui/drop_ref.stderr

+37-1
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,41 @@ note: argument has type `&SomeStruct`
107107
LL | std::mem::drop(&SomeStruct);
108108
| ^^^^^^^^^^^
109109

110-
error: aborting due to 9 previous errors
110+
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing
111+
--> $DIR/drop_ref.rs:91:13
112+
|
113+
LL | drop(println_and(&13)); // Lint, even if we only care about the side-effect, it's already in a block
114+
| ^^^^^^^^^^^^^^^^^^^^^^
115+
|
116+
note: argument has type `&i32`
117+
--> $DIR/drop_ref.rs:91:18
118+
|
119+
LL | drop(println_and(&13)); // Lint, even if we only care about the side-effect, it's already in a block
120+
| ^^^^^^^^^^^^^^^^
121+
122+
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing
123+
--> $DIR/drop_ref.rs:93:14
124+
|
125+
LL | 3 if drop(println_and(&14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
126+
| ^^^^^^^^^^^^^^^^^^^^^^
127+
|
128+
note: argument has type `&i32`
129+
--> $DIR/drop_ref.rs:93:19
130+
|
131+
LL | 3 if drop(println_and(&14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
132+
| ^^^^^^^^^^^^^^^^
133+
134+
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing
135+
--> $DIR/drop_ref.rs:94:14
136+
|
137+
LL | 4 => drop(&2), // Lint, not a fn/method call
138+
| ^^^^^^^^
139+
|
140+
note: argument has type `&i32`
141+
--> $DIR/drop_ref.rs:94:19
142+
|
143+
LL | 4 => drop(&2), // Lint, not a fn/method call
144+
| ^^
145+
146+
error: aborting due to 12 previous errors
111147

0 commit comments

Comments
 (0)